diff --git a/__tests__/Record.ts b/__tests__/Record.ts index 76ad89da86..24bbf30f80 100644 --- a/__tests__/Record.ts +++ b/__tests__/Record.ts @@ -7,7 +7,7 @@ /// -import { isKeyed, Record, Seq } from '../'; +import { isKeyed, Map, Record, Seq } from '../'; describe('Record', () => { it('defines a constructor', () => { @@ -261,4 +261,26 @@ describe('Record', () => { expect(factoryA().equals(factoryA())).toBe(true); expect(factoryA().equals(factoryB())).toBe(true); }); + + it('does not accept a Record as constructor', () => { + const Foo = Record({ foo: 'bar' }); + const fooInstance = Foo(); + expect(() => { + Record(fooInstance); + }).toThrowErrorMatchingSnapshot(); + }); + + it('does not accept a non object as constructor', () => { + const defaultValues = null; + expect(() => { + Record(defaultValues); + }).toThrowErrorMatchingSnapshot(); + }); + + it('does not accept an immutable object that is not a Record as constructor', () => { + const defaultValues = Map({ foo: 'bar' }); + expect(() => { + Record(defaultValues); + }).toThrowErrorMatchingSnapshot(); + }); }); diff --git a/__tests__/__snapshots__/Record.ts.snap b/__tests__/__snapshots__/Record.ts.snap new file mode 100644 index 0000000000..46daf60171 --- /dev/null +++ b/__tests__/__snapshots__/Record.ts.snap @@ -0,0 +1,7 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Record does not accept a Record as constructor 1`] = `"Can not call \`Record\` with an immutable Record as default values. Use a plain javascript object instead."`; + +exports[`Record does not accept a non object as constructor 1`] = `"Can not call \`Record\` with a non-object as default values. Use a plain javascript object instead."`; + +exports[`Record does not accept an immutable object that is not a Record as constructor 1`] = `"Can not call \`Record\` with an immutable Collection as default values. Use a plain javascript object instead."`; diff --git a/src/Record.js b/src/Record.js index 2731267a49..68ffec25c8 100644 --- a/src/Record.js +++ b/src/Record.js @@ -28,11 +28,34 @@ import { asImmutable } from './methods/asImmutable'; import invariant from './utils/invariant'; import quoteString from './utils/quoteString'; +import { isImmutable } from './predicates/isImmutable'; + +function throwOnInvalidDefaultValues(defaultValues) { + if (isRecord(defaultValues)) { + throw new Error( + 'Can not call `Record` with an immutable Record as default values. Use a plain javascript object instead.' + ); + } + + if (isImmutable(defaultValues)) { + throw new Error( + 'Can not call `Record` with an immutable Collection as default values. Use a plain javascript object instead.' + ); + } + + if (defaultValues === null || typeof defaultValues !== 'object') { + throw new Error( + 'Can not call `Record` with a non-object as default values. Use a plain javascript object instead.' + ); + } +} export class Record { constructor(defaultValues, name) { let hasInitialized; + throwOnInvalidDefaultValues(defaultValues); + const RecordType = function Record(values) { if (values instanceof RecordType) { return values;