mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-20 23:52:40 +08:00
67e9aa1f33
* 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>
59 lines
2.0 KiB
TypeScript
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,
|
|
});
|
|
});
|
|
});
|
|
|