|
| 1 | +import { assert, JSData, sinon } from '../../_setup' |
| 2 | + |
| 3 | +describe("Record#on('changes')", function() { |
| 4 | + it('Tracking changes to a single property', function(done) { |
| 5 | + const Store = new JSData.DataStore() |
| 6 | + const Foo = Store.defineMapper('foo', { |
| 7 | + schema: { |
| 8 | + properties: { |
| 9 | + id: { type: 'number' }, |
| 10 | + name: { type: 'string', track: true } |
| 11 | + } |
| 12 | + }, |
| 13 | + }) |
| 14 | + const foo = Foo.createRecord() |
| 15 | + const listener = sinon.stub() |
| 16 | + foo.on('change', listener) |
| 17 | + foo.name = 'new foo' |
| 18 | + const secondSpec = function() { |
| 19 | + foo.name = 'updated foo' |
| 20 | + setTimeout(function () { |
| 21 | + const [record, changes] = listener.args[1] |
| 22 | + assert.equal(foo, record, "on 'change' listener called with record which was modified") |
| 23 | + assert.isTrue(listener.calledTwice, "on 'change' listener was called when modifying a property") |
| 24 | + assert.equal(Object.keys(changes.added).length, 0) |
| 25 | + assert.equal(Object.keys(changes.removed).length, 0) |
| 26 | + assert.equal(changes.changed.name, 'updated foo', "Only the property changed was emitted in the changeSet") |
| 27 | + done() |
| 28 | + }, 5) |
| 29 | + } |
| 30 | + setTimeout(function () { |
| 31 | + const [record, changes] = listener.args[0] |
| 32 | + assert.equal(foo, record, "on 'change' listener called with record which was modified") |
| 33 | + assert.isTrue(listener.calledOnce, "on 'change' listener was called when modifying a property") |
| 34 | + assert.equal(Object.keys(changes.changed).length, 0) |
| 35 | + assert.equal(Object.keys(changes.removed).length, 0) |
| 36 | + assert.equal(changes.added.name, 'new foo', "Only the property changed was emitted in the changeSet") |
| 37 | + secondSpec() |
| 38 | + }, 5) |
| 39 | + }) |
| 40 | +}) |
0 commit comments