Skip to content
This repository was archived by the owner on Jul 23, 2021. It is now read-only.

throw Error when passing a instance of Record as a Record factory default values #195

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion __tests__/Record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

///<reference path='../resources/jest.d.ts'/>

import { isKeyed, Record, Seq } from '../';
import { isKeyed, Map, Record, Seq } from '../';

describe('Record', () => {
it('defines a constructor', () => {
Expand Down Expand Up @@ -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();
});
});
7 changes: 7 additions & 0 deletions __tests__/__snapshots__/Record.ts.snap
Original file line number Diff line number Diff line change
@@ -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."`;
23 changes: 23 additions & 0 deletions src/Record.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down