2017-04-19 00:55:46 +00:00
|
|
|
import { expect } from 'chai'
|
|
|
|
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(() => {
|
2017-04-19 00:55:46 +00:00
|
|
|
delete process.env.NODE_ENV
|
|
|
|
})
|
2015-08-02 07:15:52 +00:00
|
|
|
|
|
|
|
it('freezes objects', () => {
|
2017-04-19 00:55:46 +00:00
|
|
|
const object = {}
|
|
|
|
u.freeze(object)
|
2015-08-02 07:15:52 +00:00
|
|
|
|
2017-04-19 00:55:46 +00:00
|
|
|
expect(Object.isFrozen(object)).to.be.true
|
|
|
|
})
|
2015-08-02 07:15:52 +00:00
|
|
|
|
|
|
|
it('freezes nested objects', () => {
|
2017-04-19 00:55:46 +00:00
|
|
|
const object = { foo: { bar: 3 } }
|
|
|
|
u.freeze(object)
|
2015-08-02 07:15:52 +00:00
|
|
|
|
2017-04-19 00:55:46 +00:00
|
|
|
expect(Object.isFrozen(object.foo)).to.be.true
|
|
|
|
})
|
2015-08-02 07:15:52 +00:00
|
|
|
|
|
|
|
it('freezes nested arrays', () => {
|
2017-04-19 00:55:46 +00:00
|
|
|
const object = [[0]]
|
|
|
|
u.freeze(object)
|
2015-08-02 07:15:52 +00:00
|
|
|
|
2017-04-19 00:55:46 +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', () => {
|
2017-04-19 00:55:46 +00:00
|
|
|
const object = { foo: () => 1 }
|
|
|
|
u.freeze(object)
|
2015-08-02 07:15:52 +00:00
|
|
|
|
2017-04-19 00:55:46 +00:00
|
|
|
expect(Object.isFrozen(object.foo)).to.be.false
|
|
|
|
})
|
2015-08-02 07:15:52 +00:00
|
|
|
|
2016-07-07 01:20:59 +00:00
|
|
|
it('ignores regexps', () => {
|
2017-04-19 00:55:46 +00:00
|
|
|
const object = { foo: /\d/ }
|
|
|
|
u.freeze(object)
|
2016-07-07 01:20:59 +00:00
|
|
|
|
2017-04-19 00:55:46 +00:00
|
|
|
expect(Object.isFrozen(object.foo)).to.be.false
|
|
|
|
})
|
2016-07-07 01:20:59 +00:00
|
|
|
|
2015-08-02 07:15:52 +00:00
|
|
|
it('does not freeze children if the parent is already frozen', () => {
|
2017-04-19 00:55:46 +00:00
|
|
|
const object = { foo: {} }
|
|
|
|
Object.freeze(object)
|
|
|
|
u.freeze(object)
|
2015-08-02 07:15:52 +00:00
|
|
|
|
2017-04-19 00:55:46 +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', () => {
|
2020-04-02 14:15:02 +00:00
|
|
|
process.env = {
|
|
|
|
...process.env,
|
|
|
|
NODE_ENV: 'production',
|
|
|
|
}
|
|
|
|
|
2017-04-19 00:55:46 +00:00
|
|
|
const object = {}
|
|
|
|
u.freeze(object)
|
2015-08-02 07:15:52 +00:00
|
|
|
|
2017-04-19 00:55:46 +00:00
|
|
|
expect(Object.isFrozen(object)).to.be.false
|
|
|
|
})
|
2015-08-22 18:03:38 +00:00
|
|
|
}
|
2015-08-03 17:37:26 +00:00
|
|
|
|
|
|
|
it('handles null objects', () => {
|
2017-04-19 00:55:46 +00:00
|
|
|
const object = { foo: null }
|
|
|
|
u.freeze(object)
|
|
|
|
expect(Object.isFrozen(object)).to.be.true
|
|
|
|
})
|
2015-08-05 04:56:01 +00:00
|
|
|
|
|
|
|
it('returns the same object', () => {
|
2017-04-19 00:55:46 +00:00
|
|
|
const object = {}
|
|
|
|
const result = u.freeze(object)
|
|
|
|
expect(result).to.equal(object)
|
|
|
|
})
|
|
|
|
})
|