Skip to content

Commit

Permalink
Examples of unit tests for stores
Browse files Browse the repository at this point in the history
  • Loading branch information
fisherwebdev committed Sep 24, 2014
1 parent abcf802 commit d1db952
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 2 deletions.
44 changes: 44 additions & 0 deletions examples/flux-chat/js/stores/__tests__/UnreadThreadStore-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* This file is provided by Facebook for testing and evaluation purposes
* only. Facebook reserves all rights not expressly granted.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

jest.dontMock('../UnreadThreadStore');
jest.dontMock('react/lib/merge');

describe('UnreadThreadStore', function() {

var ChatAppDispatcher;
var UnreadThreadStore;
var callback;

beforeEach(function() {
ChatAppDispatcher = require('../../dispatcher/ChatAppDispatcher');
UnreadThreadStore = require('../UnreadThreadStore');
callback = ChatAppDispatcher.register.mock.calls[0][0];
});

it('registers a callback with the dispatcher', function() {
expect(ChatAppDispatcher.register.mock.calls.length).toBe(1);
});

it('provides the unread thread count', function() {
var ThreadStore = require('../ThreadStore');
ThreadStore.getAll.mockReturnValueOnce(
{
foo: {lastMessage: {isRead: false}},
bar: {lastMessage: {isRead: false}},
baz: {lastMessage: {isRead: true}}
}
);
expect(UnreadThreadStore.getCount()).toBe(2);
});

});
4 changes: 2 additions & 2 deletions examples/flux-todomvc/js/stores/TodoStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ var _todos = {};
function create(text) {
// Hand waving here -- not showing how this interacts with XHR or persistent
// server-side storage.
// Using the current timestamp in place of a real id.
var id = Date.now();
// Using the current timestamp + random number in place of a real id.
var id = (+new Date() + Math.floor(Math.random() * 999999)).toString(36);
_todos[id] = {
id: id,
complete: false,
Expand Down
99 changes: 99 additions & 0 deletions examples/flux-todomvc/js/stores/__tests__/TodoStore-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* TodoStore-test
*/

jest.dontMock('../../constants/TodoConstants');
jest.dontMock('../TodoStore');
jest.dontMock('react/lib/merge');

describe('TodoStore', function() {

var TodoConstants = require('../../constants/TodoConstants');

// mock actions inside dispatch payloads
var actionTodoCreate = {
source: 'VIEW_ACTION',
action: {
actionType: TodoConstants.TODO_CREATE,
text: 'foo'
}
};
var actionTodoDestroy = {
source: 'VIEW_ACTION',
action: {
actionType: TodoConstants.TODO_DESTROY,
id: 'replace me in test'
}
};

beforeEach(function() {
AppDispatcher = require('../../dispatcher/AppDispatcher');
TodoStore = require('../TodoStore');
callback = AppDispatcher.register.mock.calls[0][0];
});

it('should register a callback with the dispatcher', function() {
expect(AppDispatcher.register.mock.calls.length).toBe(1);
});

it('should initialize with no to-do items', function() {
var all = TodoStore.getAll();
expect(all).toEqual({});
});

it('creates a to-do item', function() {
callback(actionTodoCreate);
var all = TodoStore.getAll();
var keys = Object.keys(all);
expect(keys.length).toBe(1);
expect(all[keys[0]].text).toEqual('foo');
});

it('destroys a to-do item', function() {
callback(actionTodoCreate);
var all = TodoStore.getAll();
var keys = Object.keys(all);
expect(keys.length).toBe(1);
actionTodoDestroy.action.id = keys[0];
callback(actionTodoDestroy);
expect(all[keys[0]]).toBeUndefined();
});

it('can determine whether all to-do items are complete', function() {
var i = 0;
for (; i < 3; i++) {
callback(actionTodoCreate);
}
expect(Object.keys(TodoStore.getAll()).length).toBe(3);
expect(TodoStore.areAllComplete()).toBe(false);

var all = TodoStore.getAll();
for (key in all) {
callback({
source: 'VIEW_ACTION',
action: {
actionType: TodoConstants.TODO_COMPLETE,
id: key
}
});
}
expect(TodoStore.areAllComplete()).toBe(true);

callback({
source: 'VIEW_ACTION',
action: {
actionType: TodoConstants.TODO_UNDO_COMPLETE,
id: key
}
});
expect(TodoStore.areAllComplete()).toBe(false);
});

});

0 comments on commit d1db952

Please sign in to comment.