updeep-remeda/test/freeze-spec.js

68 lines
1.5 KiB
JavaScript
Raw Normal View History

import { expect } from 'chai';
2015-08-05 06:36:40 +00:00
import u from '../lib';
2015-08-02 07:15:52 +00:00
2015-08-05 05:28:31 +00:00
describe('u.freeze', () => {
2015-08-02 07:15:52 +00:00
afterEach(() => {
delete process.env.NODE_ENV;
});
it('freezes objects', () => {
2015-08-05 07:27:56 +00:00
const object = {};
u.freeze(object);
2015-08-02 07:15:52 +00:00
expect(Object.isFrozen(object)).to.be.true;
2015-08-02 07:15:52 +00:00
});
it('freezes nested objects', () => {
2015-08-05 07:27:56 +00:00
const object = { foo: { bar: 3 } };
u.freeze(object);
2015-08-02 07:15:52 +00:00
expect(Object.isFrozen(object.foo)).to.be.true;
2015-08-02 07:15:52 +00:00
});
it('freezes nested arrays', () => {
2015-08-05 07:27:56 +00:00
const object = [[0]];
u.freeze(object);
2015-08-02 07:15:52 +00:00
expect(Object.isFrozen(object)).to.be.true;
expect(Object.isFrozen(object[0])).to.be.true;
2015-08-02 07:15:52 +00:00
});
it('ignores functions', () => {
2015-08-05 07:27:56 +00:00
const object = { foo: () => 1 };
u.freeze(object);
2015-08-02 07:15:52 +00:00
expect(Object.isFrozen(object.foo)).to.be.false;
2015-08-02 07:15:52 +00:00
});
it('does not freeze children if the parent is already frozen', () => {
2015-08-05 07:27:56 +00:00
const object = { foo: {} };
Object.freeze(object);
u.freeze(object);
2015-08-02 07:15:52 +00:00
expect(Object.isFrozen(object.foo)).to.be.false;
2015-08-02 07:15:52 +00:00
});
2015-08-22 18:03:38 +00:00
if (typeof process !== 'undefined') {
it('does not freeze in production', () => {
process.env.NODE_ENV = 'production';
const object = {};
u.freeze(object);
2015-08-02 07:15:52 +00:00
2015-08-22 18:03:38 +00:00
expect(Object.isFrozen(object)).to.be.false;
});
}
2015-08-03 17:37:26 +00:00
it('handles null objects', () => {
2015-08-05 07:27:56 +00:00
const object = { foo: null };
u.freeze(object);
expect(Object.isFrozen(object)).to.be.true;
2015-08-03 17:37:26 +00:00
});
2015-08-05 04:56:01 +00:00
it('returns the same object', () => {
2015-08-05 07:27:56 +00:00
const object = {};
const result = u.freeze(object);
expect(result).to.equal(object);
2015-08-05 04:56:01 +00:00
});
2015-08-02 07:15:52 +00:00
});