Files
cocos4/tests/core/serialization/deserialize-missing-class.test.ts
T
黄森斌 67e9aa1f33 V3.6.0 allow multiple custom scripts exist in project with one same className (#11763)
* use getClassById instead of _getClassById

* use getClassId instead of _getClassId

* public cc.js.getClassId api

* allow className already exists, new one will replace old classObject

* create component use cid

* allow className exist when registing

* custom script className allow duplicated

* revert some comments

* preserve api and deprecate _getClassById and _getClassId

* revert some comments

* revert comment

* Update cocos/core/utils/js-typed.ts

Co-authored-by: PP <chinese1236@qq.com>

Co-authored-by: PP <chinese1236@qq.com>
2022-07-05 15:19:56 +08:00

59 lines
2.0 KiB
TypeScript

import { MissingScript } from '../../../cocos/core';
import { deserialize } from '../../../cocos/core/data/deserialize';
import { js } from '../../../cocos/core/utils/js';
describe(`Missing class deserialization`, () => {
const serialized = {
'__type__': '__type_that_can_never_be_found__',
foo: 2,
};
const mockReportMissingClass = jest.fn();
test(`Customized reporter`, () => {
const deserialized = deserialize(serialized, undefined, {
reportMissingClass: mockReportMissingClass,
});
expect(deserialized).toBe(null);
expect(mockReportMissingClass).toBeCalledTimes(1);
expect(mockReportMissingClass).toBeCalledWith('__type_that_can_never_be_found__');
});
test(`Customized reporter by changing deserialize.reportMissingClass`, () => {
const spy = jest.spyOn(deserialize, 'reportMissingClass');
const deserialized = deserialize(serialized, undefined, {
reportMissingClass: undefined,
});
expect(deserialized).toBe(null);
expect(spy).toBeCalledTimes(1);
expect(spy).toBeCalledWith('__type_that_can_never_be_found__');
spy.mockRestore();
});
test(`Class finder returns MissingScript`, () => {
function classFinder(type: any) {
const result = js.getClassById(type);
if (result) {
return result;
} else {
return MissingScript;
}
}
const deserialized = deserialize(serialized, undefined, {
classFinder,
reportMissingClass: mockReportMissingClass,
}) as MissingScript;
expect(mockReportMissingClass).toBeCalledTimes(1);
expect(mockReportMissingClass).toBeCalledWith('__type_that_can_never_be_found__');
expect(deserialized).toBeInstanceOf(MissingScript);
expect(deserialized._$erialized).toStrictEqual({
'__type__': '__type_that_can_never_be_found__',
foo: 2,
});
});
});