Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80698 views
1
/*
2
* Copyright (c) 2014-2015, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* TodoStore-test
10
*/
11
12
jest.dontMock('../../constants/TodoConstants');
13
jest.dontMock('../TodoStore');
14
jest.dontMock('object-assign');
15
16
describe('TodoStore', function() {
17
18
var TodoConstants = require('../../constants/TodoConstants');
19
var AppDispatcher;
20
var TodoStore;
21
var callback;
22
23
// mock actions
24
var actionTodoCreate = {
25
actionType: TodoConstants.TODO_CREATE,
26
text: 'foo'
27
};
28
var actionTodoDestroy = {
29
actionType: TodoConstants.TODO_DESTROY,
30
id: 'replace me in test'
31
};
32
33
beforeEach(function() {
34
AppDispatcher = require('../../dispatcher/AppDispatcher');
35
TodoStore = require('../TodoStore');
36
callback = AppDispatcher.register.mock.calls[0][0];
37
});
38
39
it('registers a callback with the dispatcher', function() {
40
expect(AppDispatcher.register.mock.calls.length).toBe(1);
41
});
42
43
it('should initialize with no to-do items', function() {
44
var all = TodoStore.getAll();
45
expect(all).toEqual({});
46
});
47
48
it('creates a to-do item', function() {
49
callback(actionTodoCreate);
50
var all = TodoStore.getAll();
51
var keys = Object.keys(all);
52
expect(keys.length).toBe(1);
53
expect(all[keys[0]].text).toEqual('foo');
54
});
55
56
it('destroys a to-do item', function() {
57
callback(actionTodoCreate);
58
var all = TodoStore.getAll();
59
var keys = Object.keys(all);
60
expect(keys.length).toBe(1);
61
actionTodoDestroy.id = keys[0];
62
callback(actionTodoDestroy);
63
expect(all[keys[0]]).toBeUndefined();
64
});
65
66
it('can determine whether all to-do items are complete', function() {
67
var i = 0;
68
for (; i < 3; i++) {
69
callback(actionTodoCreate);
70
}
71
expect(Object.keys(TodoStore.getAll()).length).toBe(3);
72
expect(TodoStore.areAllComplete()).toBe(false);
73
74
var all = TodoStore.getAll();
75
for (key in all) {
76
callback({
77
actionType: TodoConstants.TODO_COMPLETE,
78
id: key
79
});
80
}
81
expect(TodoStore.areAllComplete()).toBe(true);
82
83
callback({
84
actionType: TodoConstants.TODO_UNDO_COMPLETE,
85
id: key
86
});
87
expect(TodoStore.areAllComplete()).toBe(false);
88
});
89
90
});
91
92