Skip to content

Commit

Permalink
fix: clear BaseRecord#populated when value is undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtek-krysiak committed Oct 1, 2020
1 parent 6915056 commit 54dae81
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -26,3 +26,4 @@ app/cypress/videos
app/cypress/screenshots
app/build
.env
yarn-error.log
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -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",
Expand Down
11 changes: 11 additions & 0 deletions 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'
Expand All @@ -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
Expand Down Expand Up @@ -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 })
})
})
})
16 changes: 11 additions & 5 deletions src/backend/adapters/record/base-record.ts
Expand Up @@ -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
}
}

/**
Expand Down
11 changes: 11 additions & 0 deletions 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;
}
}
5 changes: 5 additions & 0 deletions yarn.lock
Expand Up @@ -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"
Expand Down

0 comments on commit 54dae81

Please sign in to comment.