From 103a170917f0ff313e838bf8a886556814ffa483 Mon Sep 17 00:00:00 2001 From: chenos Date: Fri, 7 Jan 2022 21:47:10 +0800 Subject: [PATCH] add test case for magic-attribute-model --- .../__tests__/magic-attribute-model.test.ts | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 packages/database/src/__tests__/magic-attribute-model.test.ts diff --git a/packages/database/src/__tests__/magic-attribute-model.test.ts b/packages/database/src/__tests__/magic-attribute-model.test.ts new file mode 100644 index 000000000..d9ed1ee2b --- /dev/null +++ b/packages/database/src/__tests__/magic-attribute-model.test.ts @@ -0,0 +1,43 @@ +import { mockDatabase } from '.'; +import { MagicAttributeModel } from '..'; + +describe('magic-attribute-model', () => { + it('case 1', async () => { + const db = mockDatabase(); + db.registerModels({ MagicAttributeModel }); + + const Test = db.collection({ + name: 'tests', + model: 'MagicAttributeModel', + fields: [ + { type: 'string', name: 'title' }, + { type: 'json', name: 'options' }, + ], + }); + + await db.sync(); + + const test = await Test.model.create({ + title: 'aa', + 'x-component-props': { key1: 'val1', arr1: [1, 2, 3], arr2: [4, 5] }, + }); + + test.set({ + 'x-component-props': { key2: 'val2', arr1: [3, 4] }, + 'x-decorator-props': { key1: 'val1' }, + }); + + test.set('x-component-props', { arr2: [1, 2, 3] }); + + expect(test.toJSON()).toMatchObject({ + title: 'aa', + 'x-component-props': { + key1: 'val1', + key2: 'val2', + arr1: [3, 4], + arr2: [1, 2, 3], + }, + 'x-decorator-props': { key1: 'val1' }, + }); + }); +});