1 |
|
2 |
|
3 | var assert = require('assert');
|
4 | var after = require('after');
|
5 |
|
6 | var Model = require('../model');
|
7 |
|
8 | suite('events');
|
9 |
|
10 |
|
11 | var Post = Model({
|
12 | title: String,
|
13 | author: {
|
14 | name: String,
|
15 | email: String
|
16 | }
|
17 | });
|
18 |
|
19 | test('top level changes', function(done) {
|
20 | var post = Post();
|
21 |
|
22 | post.once('change title', function(val) {
|
23 | assert.equal(val, 'foobar');
|
24 | assert.equal(post.title, 'foobar');
|
25 | done();
|
26 | });
|
27 |
|
28 | post.title = 'foobar';
|
29 | });
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | test('nested change - primary', function(done) {
|
36 | var post = Post();
|
37 |
|
38 |
|
39 |
|
40 | assert.ok(post.author === undefined);
|
41 |
|
42 | done = after(2, done);
|
43 |
|
44 | post.once('change author', function(val) {
|
45 | assert.deepEqual(val, { name: undefined, email: undefined });
|
46 | assert.equal(post.author.name, undefined);
|
47 | done();
|
48 | });
|
49 |
|
50 |
|
51 | post.author = {};
|
52 |
|
53 | post.once('change author.name', function(val) {
|
54 | assert.equal(val, 'Edgar Poe');
|
55 | assert.equal(post.author.name, 'Edgar Poe');
|
56 | done();
|
57 | });
|
58 |
|
59 |
|
60 | post.author.name = 'Edgar Poe';
|
61 | });
|
62 |
|
63 |
|
64 |
|
65 | test('nested change - field', function(done) {
|
66 | var post = Post();
|
67 |
|
68 |
|
69 |
|
70 | assert.ok(post.author === undefined);
|
71 |
|
72 | post.once('change author', function(val) {
|
73 | assert.equal(val.name, 'Edgar Poe');
|
74 | assert.equal(post.author.name, 'Edgar Poe');
|
75 | done();
|
76 | });
|
77 |
|
78 |
|
79 | post.author = {
|
80 | name: 'Edgar Poe'
|
81 | };
|
82 | });
|
83 |
|
84 |
|
85 |
|
86 | test('obj change then trigger', function(done) {
|
87 | var post = Post({
|
88 | author: {
|
89 | name: 'foobar'
|
90 | }
|
91 | });
|
92 |
|
93 | assert.ok(post.author);
|
94 | assert.equal(post.author.name, 'foobar');
|
95 |
|
96 | post.once('change author', function(val) {
|
97 | assert.equal(val.name, 'Poe');
|
98 | assert.equal(post.author.name, 'Poe');
|
99 | assert.equal(val.email, 'poe@example.com');
|
100 | assert.equal(post.author.email, 'poe@example.com');
|
101 | });
|
102 |
|
103 | post.author = {
|
104 | name: 'Poe',
|
105 | email: 'poe@example.com'
|
106 | };
|
107 |
|
108 |
|
109 |
|
110 | post.once('change author.email', function(val) {
|
111 | assert.equal(val, 'edgar@example.com');
|
112 | assert.equal(post.author.email, 'edgar@example.com');
|
113 | done();
|
114 | });
|
115 |
|
116 |
|
117 | post.author.email = 'edgar@example.com';
|
118 | });
|