n8n/packages/workflow/test/ObservableObject.test.ts

172 lines
7.7 KiB
TypeScript
Raw Normal View History

:art: Set up linting and formatting (#2120) * :arrow_up: Upgrade TS to 4.3.5 * :shirt: Add ESLint configs * :art: Add Prettier config * :package: Add deps and commands * :zap: Adjust global .editorconfig to new ruleset * :fire: Remove unneeded local .editorconfig * :package: Update deps in editor-ui * :hammer: Limit Prettier to only TS files * :zap: Add recommended VSCode extensions * :shirt: Fix build * :fire: Remove Vue setting from global config * :zap: Disable prefer-default-export per feedback * :pencil2: Add forgotten divider * :shirt: Disable no-plusplus * :shirt: Disable class-methods-use-this * :pencil2: Alphabetize overrides * :shirt: Add one-var consecutive override * :rewind: Revert one-var consecutive override This reverts commit b9252cf935659ba6d76727ad484a1d3c00008fcc. * 🎨 👕 Lint and format workflow package (#2121) * :art: Format /workflow package * :shirt: Lint /workflow package * :art: Re-format /workflow package * :shirt: Re-lint /workflow package * :pencil2: Fix typo * :zap: Consolidate if-checks * :fire: Remove prefer-default-export exceptions * :fire: Remove no-plusplus exceptions * :fire: Remove class-methods-use-this exceptions * 🎨 👕 Lint and format node-dev package (#2122) * :art: Format /node-dev package * :zap: Exclude templates from ESLint config This keeps the templates consistent with the codebase while preventing lint exceptions from being made part of the templates. * :shirt: Lint /node-dev package * :fire: Remove prefer-default-export exceptions * :fire: Remove no-plusplus exceptions * 🎨 👕 Lint and format core package (#2123) * :art: Format /core package * :shirt: Lint /core package * :art: Re-format /core package * :shirt: Re-lint /core package * :fire: Remove prefer-default-export exceptions * :fire: Remove no-plusplus exceptions * :fire: Remove class-methods-use-this exceptions * 🎨 👕 Lint and format cli package (#2124) * :art: Format /cli package * :shirt: Exclude migrations from linting * :shirt: Lint /cli package * :art: Re-format /cli package * :shirt: Re-lint /cli package * :shirt: Fix build * :fire: Remove prefer-default-export exceptions * :zap: Update exceptions in ActiveExecutions * :fire: Remove no-plusplus exceptions * :fire: Remove class-methods-use-this exceptions * 👕 fix lint issues * :wrench: use package specific linter, remove tslint command * :hammer: resolve build issue, sync dependencies * :wrench: change lint command Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com>
2021-08-29 11:58:11 -07:00
import { IDataObject, ObservableObject } from '../src';
2019-06-23 03:35:23 -07:00
describe('ObservableObject', () => {
test('should recognize that item on parent level got added (init empty)', () => {
const testObject = ObservableObject.create({});
expect(testObject.__dataChanged).toBeFalsy();
testObject.a = {};
expect(testObject.__dataChanged).toBeTruthy();
// Make sure that "__dataChanged" does not returned as a key
expect(Object.keys(testObject)).toEqual(['a']);
});
test('should not recognize that item on parent level changed if it is empty object and option "ignoreEmptyOnFirstChild" === true (init empty)', () => {
const testObject = ObservableObject.create({}, undefined, { ignoreEmptyOnFirstChild: true });
expect(testObject.__dataChanged).toBeFalsy();
testObject.a = {};
expect(testObject.__dataChanged).toBeFalsy();
expect(testObject.a).toEqual({});
});
test('should recognize that item on parent level changed if it is not empty object and option "ignoreEmptyOnFirstChild" === true (init empty)', () => {
const testObject = ObservableObject.create({}, undefined, { ignoreEmptyOnFirstChild: true });
expect(testObject.__dataChanged).toBeFalsy();
testObject.a = { b: 2 };
expect(testObject.__dataChanged).toBeTruthy();
expect(testObject.a).toEqual({ b: 2 });
});
test('should not recognize that item on parent level changed if it is empty array and option "ignoreEmptyOnFirstChild" === true (init empty)', () => {
const testObject = ObservableObject.create({}, undefined, { ignoreEmptyOnFirstChild: true });
expect(testObject.__dataChanged).toBeFalsy();
testObject.a = [];
expect(testObject.__dataChanged).toBeFalsy();
expect(testObject.a).toEqual([]);
});
test('should recognize that item on parent level changed if it is not empty []] and option "ignoreEmptyOnFirstChild" === true (init empty)', () => {
const testObject = ObservableObject.create({}, undefined, { ignoreEmptyOnFirstChild: true });
expect(testObject.__dataChanged).toBeFalsy();
testObject.a = [1, 2];
expect(testObject.__dataChanged).toBeTruthy();
expect(testObject.a).toEqual([1, 2]);
});
test('should recognize that item on parent level changed (init data exists)', () => {
const testObject = ObservableObject.create({ a: 1 });
expect(testObject.__dataChanged).toBeFalsy();
expect(testObject.a).toEqual(1);
testObject.a = 2;
expect(testObject.__dataChanged).toBeTruthy();
expect(testObject.a).toEqual(2);
});
test('should recognize that array on parent level changed (init data exists)', () => {
const testObject = ObservableObject.create({ a: [1, 2] });
expect(testObject.__dataChanged).toBeFalsy();
expect(testObject.a).toEqual([1, 2]);
(testObject.a as number[]).push(3);
expect(testObject.__dataChanged).toBeTruthy();
expect(testObject.a).toEqual([1, 2, 3]);
});
test('should recognize that item on first child level changed (init data exists)', () => {
const testObject = ObservableObject.create({ a: { b: 1 } });
expect(testObject.__dataChanged).toBeFalsy();
expect((testObject.a! as IDataObject).b).toEqual(1);
(testObject.a! as IDataObject).b = 2;
expect(testObject.__dataChanged).toBeTruthy();
expect((testObject.a! as IDataObject).b).toEqual(2);
});
test('should recognize that item on first child level changed if it is now empty and option "ignoreEmptyOnFirstChild" === true (init data exists)', () => {
:art: Set up linting and formatting (#2120) * :arrow_up: Upgrade TS to 4.3.5 * :shirt: Add ESLint configs * :art: Add Prettier config * :package: Add deps and commands * :zap: Adjust global .editorconfig to new ruleset * :fire: Remove unneeded local .editorconfig * :package: Update deps in editor-ui * :hammer: Limit Prettier to only TS files * :zap: Add recommended VSCode extensions * :shirt: Fix build * :fire: Remove Vue setting from global config * :zap: Disable prefer-default-export per feedback * :pencil2: Add forgotten divider * :shirt: Disable no-plusplus * :shirt: Disable class-methods-use-this * :pencil2: Alphabetize overrides * :shirt: Add one-var consecutive override * :rewind: Revert one-var consecutive override This reverts commit b9252cf935659ba6d76727ad484a1d3c00008fcc. * 🎨 👕 Lint and format workflow package (#2121) * :art: Format /workflow package * :shirt: Lint /workflow package * :art: Re-format /workflow package * :shirt: Re-lint /workflow package * :pencil2: Fix typo * :zap: Consolidate if-checks * :fire: Remove prefer-default-export exceptions * :fire: Remove no-plusplus exceptions * :fire: Remove class-methods-use-this exceptions * 🎨 👕 Lint and format node-dev package (#2122) * :art: Format /node-dev package * :zap: Exclude templates from ESLint config This keeps the templates consistent with the codebase while preventing lint exceptions from being made part of the templates. * :shirt: Lint /node-dev package * :fire: Remove prefer-default-export exceptions * :fire: Remove no-plusplus exceptions * 🎨 👕 Lint and format core package (#2123) * :art: Format /core package * :shirt: Lint /core package * :art: Re-format /core package * :shirt: Re-lint /core package * :fire: Remove prefer-default-export exceptions * :fire: Remove no-plusplus exceptions * :fire: Remove class-methods-use-this exceptions * 🎨 👕 Lint and format cli package (#2124) * :art: Format /cli package * :shirt: Exclude migrations from linting * :shirt: Lint /cli package * :art: Re-format /cli package * :shirt: Re-lint /cli package * :shirt: Fix build * :fire: Remove prefer-default-export exceptions * :zap: Update exceptions in ActiveExecutions * :fire: Remove no-plusplus exceptions * :fire: Remove class-methods-use-this exceptions * 👕 fix lint issues * :wrench: use package specific linter, remove tslint command * :hammer: resolve build issue, sync dependencies * :wrench: change lint command Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com>
2021-08-29 11:58:11 -07:00
const testObject = ObservableObject.create({ a: { b: 1 } }, undefined, {
ignoreEmptyOnFirstChild: true,
});
2019-06-23 03:35:23 -07:00
expect(testObject.__dataChanged).toBeFalsy();
expect((testObject.a! as IDataObject).b).toEqual(1);
testObject.a = {};
expect(testObject.__dataChanged).toBeTruthy();
expect(testObject.a).toEqual({});
});
test('should recognize that item on first child level changed if it is now empty and option "ignoreEmptyOnFirstChild" === false (init data exists)', () => {
:art: Set up linting and formatting (#2120) * :arrow_up: Upgrade TS to 4.3.5 * :shirt: Add ESLint configs * :art: Add Prettier config * :package: Add deps and commands * :zap: Adjust global .editorconfig to new ruleset * :fire: Remove unneeded local .editorconfig * :package: Update deps in editor-ui * :hammer: Limit Prettier to only TS files * :zap: Add recommended VSCode extensions * :shirt: Fix build * :fire: Remove Vue setting from global config * :zap: Disable prefer-default-export per feedback * :pencil2: Add forgotten divider * :shirt: Disable no-plusplus * :shirt: Disable class-methods-use-this * :pencil2: Alphabetize overrides * :shirt: Add one-var consecutive override * :rewind: Revert one-var consecutive override This reverts commit b9252cf935659ba6d76727ad484a1d3c00008fcc. * 🎨 👕 Lint and format workflow package (#2121) * :art: Format /workflow package * :shirt: Lint /workflow package * :art: Re-format /workflow package * :shirt: Re-lint /workflow package * :pencil2: Fix typo * :zap: Consolidate if-checks * :fire: Remove prefer-default-export exceptions * :fire: Remove no-plusplus exceptions * :fire: Remove class-methods-use-this exceptions * 🎨 👕 Lint and format node-dev package (#2122) * :art: Format /node-dev package * :zap: Exclude templates from ESLint config This keeps the templates consistent with the codebase while preventing lint exceptions from being made part of the templates. * :shirt: Lint /node-dev package * :fire: Remove prefer-default-export exceptions * :fire: Remove no-plusplus exceptions * 🎨 👕 Lint and format core package (#2123) * :art: Format /core package * :shirt: Lint /core package * :art: Re-format /core package * :shirt: Re-lint /core package * :fire: Remove prefer-default-export exceptions * :fire: Remove no-plusplus exceptions * :fire: Remove class-methods-use-this exceptions * 🎨 👕 Lint and format cli package (#2124) * :art: Format /cli package * :shirt: Exclude migrations from linting * :shirt: Lint /cli package * :art: Re-format /cli package * :shirt: Re-lint /cli package * :shirt: Fix build * :fire: Remove prefer-default-export exceptions * :zap: Update exceptions in ActiveExecutions * :fire: Remove no-plusplus exceptions * :fire: Remove class-methods-use-this exceptions * 👕 fix lint issues * :wrench: use package specific linter, remove tslint command * :hammer: resolve build issue, sync dependencies * :wrench: change lint command Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com>
2021-08-29 11:58:11 -07:00
const testObject = ObservableObject.create({ a: { b: 1 } }, undefined, {
ignoreEmptyOnFirstChild: false,
});
2019-06-23 03:35:23 -07:00
expect(testObject.__dataChanged).toBeFalsy();
expect((testObject.a! as IDataObject).b).toEqual(1);
testObject.a = {};
expect(testObject.__dataChanged).toBeTruthy();
expect(testObject.a).toEqual({});
});
test('should recognize that array on first child level changed (init data exists)', () => {
const testObject = ObservableObject.create({ a: { b: [1, 2] } });
expect(testObject.__dataChanged).toBeFalsy();
expect((testObject.a! as IDataObject).b).toEqual([1, 2]);
((testObject.a! as IDataObject).b as number[]).push(3);
expect(testObject.__dataChanged).toBeTruthy();
expect((testObject.a! as IDataObject).b).toEqual([1, 2, 3]);
});
test('should recognize that item on second child level changed (init data exists)', () => {
const testObject = ObservableObject.create({ a: { b: { c: 1 } } });
expect(testObject.__dataChanged).toBeFalsy();
:art: Set up linting and formatting (#2120) * :arrow_up: Upgrade TS to 4.3.5 * :shirt: Add ESLint configs * :art: Add Prettier config * :package: Add deps and commands * :zap: Adjust global .editorconfig to new ruleset * :fire: Remove unneeded local .editorconfig * :package: Update deps in editor-ui * :hammer: Limit Prettier to only TS files * :zap: Add recommended VSCode extensions * :shirt: Fix build * :fire: Remove Vue setting from global config * :zap: Disable prefer-default-export per feedback * :pencil2: Add forgotten divider * :shirt: Disable no-plusplus * :shirt: Disable class-methods-use-this * :pencil2: Alphabetize overrides * :shirt: Add one-var consecutive override * :rewind: Revert one-var consecutive override This reverts commit b9252cf935659ba6d76727ad484a1d3c00008fcc. * 🎨 👕 Lint and format workflow package (#2121) * :art: Format /workflow package * :shirt: Lint /workflow package * :art: Re-format /workflow package * :shirt: Re-lint /workflow package * :pencil2: Fix typo * :zap: Consolidate if-checks * :fire: Remove prefer-default-export exceptions * :fire: Remove no-plusplus exceptions * :fire: Remove class-methods-use-this exceptions * 🎨 👕 Lint and format node-dev package (#2122) * :art: Format /node-dev package * :zap: Exclude templates from ESLint config This keeps the templates consistent with the codebase while preventing lint exceptions from being made part of the templates. * :shirt: Lint /node-dev package * :fire: Remove prefer-default-export exceptions * :fire: Remove no-plusplus exceptions * 🎨 👕 Lint and format core package (#2123) * :art: Format /core package * :shirt: Lint /core package * :art: Re-format /core package * :shirt: Re-lint /core package * :fire: Remove prefer-default-export exceptions * :fire: Remove no-plusplus exceptions * :fire: Remove class-methods-use-this exceptions * 🎨 👕 Lint and format cli package (#2124) * :art: Format /cli package * :shirt: Exclude migrations from linting * :shirt: Lint /cli package * :art: Re-format /cli package * :shirt: Re-lint /cli package * :shirt: Fix build * :fire: Remove prefer-default-export exceptions * :zap: Update exceptions in ActiveExecutions * :fire: Remove no-plusplus exceptions * :fire: Remove class-methods-use-this exceptions * 👕 fix lint issues * :wrench: use package specific linter, remove tslint command * :hammer: resolve build issue, sync dependencies * :wrench: change lint command Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com>
2021-08-29 11:58:11 -07:00
expect((testObject.a! as IDataObject).b).toEqual({ c: 1 });
2019-06-23 03:35:23 -07:00
expect(((testObject.a! as IDataObject).b! as IDataObject).c).toEqual(1);
((testObject.a! as IDataObject).b! as IDataObject).c = 2;
expect(testObject.__dataChanged).toBeTruthy();
expect((testObject.a! as IDataObject).b).toEqual({ c: 2 });
});
test('should recognize that item on parent level got deleted (init data exists)', () => {
const testObject = ObservableObject.create({ a: 1 });
expect(testObject.__dataChanged).toBeFalsy();
expect(testObject.a!).toEqual(1);
delete testObject.a;
expect(testObject.__dataChanged).toBeTruthy();
expect(testObject.a!).toEqual(undefined);
expect(testObject).toEqual({});
});
test('should recognize that item on parent level got deleted even with and option "ignoreEmptyOnFirstChild" === true (init data exists)', () => {
:art: Set up linting and formatting (#2120) * :arrow_up: Upgrade TS to 4.3.5 * :shirt: Add ESLint configs * :art: Add Prettier config * :package: Add deps and commands * :zap: Adjust global .editorconfig to new ruleset * :fire: Remove unneeded local .editorconfig * :package: Update deps in editor-ui * :hammer: Limit Prettier to only TS files * :zap: Add recommended VSCode extensions * :shirt: Fix build * :fire: Remove Vue setting from global config * :zap: Disable prefer-default-export per feedback * :pencil2: Add forgotten divider * :shirt: Disable no-plusplus * :shirt: Disable class-methods-use-this * :pencil2: Alphabetize overrides * :shirt: Add one-var consecutive override * :rewind: Revert one-var consecutive override This reverts commit b9252cf935659ba6d76727ad484a1d3c00008fcc. * 🎨 👕 Lint and format workflow package (#2121) * :art: Format /workflow package * :shirt: Lint /workflow package * :art: Re-format /workflow package * :shirt: Re-lint /workflow package * :pencil2: Fix typo * :zap: Consolidate if-checks * :fire: Remove prefer-default-export exceptions * :fire: Remove no-plusplus exceptions * :fire: Remove class-methods-use-this exceptions * 🎨 👕 Lint and format node-dev package (#2122) * :art: Format /node-dev package * :zap: Exclude templates from ESLint config This keeps the templates consistent with the codebase while preventing lint exceptions from being made part of the templates. * :shirt: Lint /node-dev package * :fire: Remove prefer-default-export exceptions * :fire: Remove no-plusplus exceptions * 🎨 👕 Lint and format core package (#2123) * :art: Format /core package * :shirt: Lint /core package * :art: Re-format /core package * :shirt: Re-lint /core package * :fire: Remove prefer-default-export exceptions * :fire: Remove no-plusplus exceptions * :fire: Remove class-methods-use-this exceptions * 🎨 👕 Lint and format cli package (#2124) * :art: Format /cli package * :shirt: Exclude migrations from linting * :shirt: Lint /cli package * :art: Re-format /cli package * :shirt: Re-lint /cli package * :shirt: Fix build * :fire: Remove prefer-default-export exceptions * :zap: Update exceptions in ActiveExecutions * :fire: Remove no-plusplus exceptions * :fire: Remove class-methods-use-this exceptions * 👕 fix lint issues * :wrench: use package specific linter, remove tslint command * :hammer: resolve build issue, sync dependencies * :wrench: change lint command Co-authored-by: Ben Hesseldieck <b.hesseldieck@gmail.com>
2021-08-29 11:58:11 -07:00
const testObject = ObservableObject.create({ a: 1 }, undefined, {
ignoreEmptyOnFirstChild: true,
});
2019-06-23 03:35:23 -07:00
expect(testObject.__dataChanged).toBeFalsy();
expect(testObject.a!).toEqual(1);
delete testObject.a;
expect(testObject.__dataChanged).toBeTruthy();
expect(testObject.a!).toEqual(undefined);
expect(testObject).toEqual({});
});
test('should recognize that item on second child level got deleted (init data exists)', () => {
const testObject = ObservableObject.create({ a: { b: { c: 1 } } });
expect(testObject.__dataChanged).toBeFalsy();
expect((testObject.a! as IDataObject).b).toEqual({ c: 1 });
delete (testObject.a! as IDataObject).b;
expect(testObject.__dataChanged).toBeTruthy();
expect((testObject.a! as IDataObject).b).toEqual(undefined);
expect(testObject).toEqual({ a: {} });
});
test('should recognize that item on second child level changed with null (init data exists)', () => {
const testObject = ObservableObject.create({ a: { b: { c: null } } });
expect(testObject.__dataChanged).toBeFalsy();
expect((testObject.a! as IDataObject).b).toEqual({ c: null });
expect(((testObject.a! as IDataObject).b! as IDataObject).c).toEqual(null);
((testObject.a! as IDataObject).b! as IDataObject).c = 2;
expect(testObject.__dataChanged).toBeTruthy();
expect((testObject.a! as IDataObject).b).toEqual({ c: 2 });
});
2019-06-23 03:35:23 -07:00
// test('xxxxxx', () => {
// const testObject = ObservableObject.create({ a: { } }, undefined, { ignoreEmptyOnFirstChild: true });
// expect(testObject.__dataChanged).toBeFalsy();
// expect(testObject).toEqual({ a: { b: { c: 1 } } });
// ((testObject.a! as DataObject).b as DataObject).c = 2;
// // expect((testObject.a! as DataObject).b).toEqual({ c: 1 });
// expect(testObject.__dataChanged).toBeTruthy();
// // expect(testObject.a).toEqual({});
// // expect((testObject.a! as DataObject).b).toEqual({ c: 1 });
// // expect(((testObject.a! as DataObject).b! as DataObject).c).toEqual(1);
// // ((testObject.a! as DataObject).b! as DataObject).c = 2;
// // expect((testObject.a! as DataObject).b).toEqual({ c: 2 });
// });
});