8000 Add additional test in hasChanges for event timing · js-data/js-data@9439fc8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9439fc8

Browse files
committed
Add additional test in hasChanges for event timing
1 parent e57ce8d commit 9439fc8

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

test/unit/record/hasChanges.test.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assert, JSData } from '../../_setup'
1+
import { assert, JSData, sinon } from '../../_setup'
22

33
describe('Record#hasChanges', function () {
44
it('should be an instance method', function () {
@@ -13,7 +13,7 @@ describe('Record#hasChanges', function () {
1313
post.author = 'Jake'
1414
assert(post.hasChanges())
1515
})
16-
it('should return true if a tracked field is changed', function () {
16+
it('should return true if a tracked field is changed', function (done) {
1717
const PostMapper = new JSData.Mapper({
1818
name: 'post',
1919
schema: {
@@ -26,10 +26,45 @@ describe('Record#hasChanges', function () {
2626
}
2727
})
2828
const post = PostMapper.createRecord(this.data.p1)
29+
const listener = sinon.stub()
30+
post.on('change', listener)
2931
assert(!post.hasChanges())
3032
post.author = 'Jake'
3133
assert(post.hasChanges())
3234
post.author = 'John'
3335
assert(!post.hasChanges())
36+
setTimeout(function() {
37+
assert.equal(listener.callCount, 0)
38+
done()
39+
}, 5)
40+
}),
41+
42+
it('is not affected by timing', function(done) {
43+
const PostMapper = new JSData.Mapper({
44+
name: 'post',
45+
schema: {
46+
properties: {
47+
author: {
48+
type: 'string',
49+
track: true
50+
}
51+
}
52+
}
53+
})
54+
const post = PostMapper.createRecord(this.data.p1)
55+
const listener = sinon.stub()
56+
post.on('change', listener)
57+
post.author = 'Jake'
58+
assert(post.hasChanges())
59+
const secondSpec = function() {
60+
assert.equal(listener.callCount, 2)
61+
assert(!post.hasChanges())
62+
done()
63+
}
64+
setTimeout(function () {
65+
assert.equal(listener.callCount, 1)
66+
post.author = 'John'
67+
setTimeout(secondSpec, 5)
68+
}, 5)
3469
})
3570
})

0 commit comments

Comments
 (0)
0