From 958eceec56382418ba3bcf067b4aafbe7ef8f1bc Mon Sep 17 00:00:00 2001 From: Ben Gotow Date: Mon, 11 May 2015 14:18:14 -0700 Subject: [PATCH] fix(object-freezing): Null is an `object`, but not a freezable one Resolves T1141 --- spec-inbox/utils-spec.coffee | 16 ++++++++++++++++ src/flux/models/utils.coffee | 5 +++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/spec-inbox/utils-spec.coffee b/spec-inbox/utils-spec.coffee index ed5904fb1..a1e9e706e 100644 --- a/spec-inbox/utils-spec.coffee +++ b/spec-inbox/utils-spec.coffee @@ -1,5 +1,7 @@ _ = require('underscore-plus') Utils = require '../src/flux/models/utils' +Thread = require '../src/flux/models/thread' +Contact = require '../src/flux/models/contact' class Foo constructor: (@instanceVar) -> @@ -13,6 +15,20 @@ class Bar extends Foo @moreStuff = stuff @method(stuff) +describe "modelFreeze", -> + it "should freeze the object", -> + o = + a: 1 + b: 2 + Utils.modelFreeze(o) + expect(Object.isFrozen(o)).toBe(true) + + it "should not throw an exception when nulls appear in strange places", -> + t = new Thread(participants: [new Contact(email: 'ben@nylas.com'), null], subject: '123') + Utils.modelFreeze(t) + expect(Object.isFrozen(t)).toBe(true) + expect(Object.isFrozen(t.participants[0])).toBe(true) + describe "deepClone", -> beforeEach -> @v1 = [1,2,3] diff --git a/src/flux/models/utils.coffee b/src/flux/models/utils.coffee index 0e8c1d579..63907536b 100644 --- a/src/flux/models/utils.coffee +++ b/src/flux/models/utils.coffee @@ -114,8 +114,9 @@ Utils = modelFreeze: (o) -> Object.freeze(o) for key, prop of o - if !o.hasOwnProperty(key) || typeof prop isnt 'object' || Object.isFrozen(prop) - continue + continue unless o.hasOwnProperty(key) + continue unless typeof prop is 'object' and prop isnt null + continue if Object.isFrozen(prop) Utils.modelFreeze(prop) modelReviver: (k, v) ->