diff --git a/.gitignore b/.gitignore index 0010fbf09..1d07542c1 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,4 @@ app/cypress/videos app/cypress/screenshots app/build .env +yarn-error.log diff --git a/package.json b/package.json index 896980f26..b52475e88 100644 --- a/package.json +++ b/package.json @@ -132,6 +132,7 @@ "@typescript-eslint/parser": "^2.3.1", "chai": "^4.1.2", "chai-as-promised": "^7.1.1", + "chai-change": "^2.1.2", "concurrently": "^5.1.0", "core-js": "2.5.7", "cspell": "^4.0.44", diff --git a/src/backend/adapters/record/base-record.spec.ts b/src/backend/adapters/record/base-record.spec.ts index 317ba22c3..826c439fa 100644 --- a/src/backend/adapters/record/base-record.spec.ts +++ b/src/backend/adapters/record/base-record.spec.ts @@ -1,4 +1,5 @@ import chai, { expect } from 'chai' +import chaiChange from 'chai-change' import sinon from 'sinon' import chaiAsPromised from 'chai-as-promised' import { ParamsType } from './params.type' @@ -9,6 +10,7 @@ import BaseProperty from '../property/base-property' import ValidationError, { PropertyErrors } from '../../utils/errors/validation-error' chai.use(chaiAsPromised) +chai.use(chaiChange) describe('Record', function () { let record: BaseRecord @@ -211,5 +213,14 @@ describe('Record', function () { expect((record as any).populated.value).to.equal(populated.value) }) + + it('clears populated field when record is null or undefined', () => { + record = new BaseRecord(params, {} as BaseResource) + record.populate('value', 'something' as any) + + expect(() => { + record.populate('value', null) + }).to.alter(() => record.populated.value, { from: 'something', to: undefined }) + }) }) }) diff --git a/src/backend/adapters/record/base-record.ts b/src/backend/adapters/record/base-record.ts index a27b4b26b..8480b4d8f 100644 --- a/src/backend/adapters/record/base-record.ts +++ b/src/backend/adapters/record/base-record.ts @@ -164,11 +164,17 @@ class BaseRecord { /** * Populate record relations * - * @param {string} propertyPath name of the property which should be populated - * @param {BaseRecord} record record to which property relates - */ - populate(propertyPath: string, record: BaseRecord): void { - this.populated[propertyPath] = record + * @param {string} propertyPath name of the property which should be populated + * @param {BaseRecord | null} [record] record to which property relates. If record is null + * or undefined - function clears the previous value + */ + populate(propertyPath: string, record?: BaseRecord | null): void { + if (record === null || typeof record === 'undefined') { + const { [propertyPath]: oldValue, ...rest } = this.populated + this.populated = rest + } else { + this.populated[propertyPath] = record + } } /** diff --git a/vendor-types/chai-change/index.d.ts b/vendor-types/chai-change/index.d.ts new file mode 100644 index 000000000..2db2e3698 --- /dev/null +++ b/vendor-types/chai-change/index.d.ts @@ -0,0 +1,11 @@ +declare namespace Chai { + type AlterOptions = { + from: any; + to: any; + by?: any; + } + + interface Assertion { + alter: (value: () => any, options?: AlterOptions) => Chai.Assertion; + } +} diff --git a/yarn.lock b/yarn.lock index ddddad348..da31080b8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2917,6 +2917,11 @@ chai-as-promised@^7.1.1: dependencies: check-error "^1.0.2" +chai-change@^2.1.2: + version "2.1.2" + resolved "https://registry.yarnpkg.com/chai-change/-/chai-change-2.1.2.tgz#1231cdf8bf5930eea1fab72b5cc4864e5bcae7f6" + integrity sha1-EjHN+L9ZMO6h+rcrXMSGTlvK5/Y= + chai@^4.1.2: version "4.2.0" resolved "https://registry.yarnpkg.com/chai/-/chai-4.2.0.tgz#760aa72cf20e3795e84b12877ce0e83737aa29e5"