[Assorted-commits] SF.net SVN: assorted:[1890] sandbox/trunk/src/node
Brought to you by:
yangzhang
From: <yan...@us...> - 2016-03-07 01:10:09
|
Revision: 1890 http://sourceforge.net/p/assorted/svn/1890 Author: yangzhang Date: 2016-03-07 01:10:08 +0000 (Mon, 07 Mar 2016) Log Message: ----------- Add jest demo Added Paths: ----------- sandbox/trunk/src/node/jest/ sandbox/trunk/src/node/jest/__tests__/ sandbox/trunk/src/node/jest/__tests__/displayUser-test.js sandbox/trunk/src/node/jest/__tests__/fetchCurrentUser-test.js sandbox/trunk/src/node/jest/__tests__/sum-test.js sandbox/trunk/src/node/jest/displayUser.js sandbox/trunk/src/node/jest/fetchCurrentUser.js sandbox/trunk/src/node/jest/package.json sandbox/trunk/src/node/jest/sum.js Added: sandbox/trunk/src/node/jest/__tests__/displayUser-test.js =================================================================== --- sandbox/trunk/src/node/jest/__tests__/displayUser-test.js (rev 0) +++ sandbox/trunk/src/node/jest/__tests__/displayUser-test.js 2016-03-07 01:10:08 UTC (rev 1890) @@ -0,0 +1,41 @@ +// __tests__/displayUser-test.js +'use strict'; + +jest + .unmock('../displayUser.js') + .unmock('jquery'); + +describe('displayUser', () => { + it('displays a user after a click', () => { + // Set up our document body + document.body.innerHTML = + '<div>' + + ' <span id="username" />' + + ' <button id="button" />' + + '</div>'; + + // This module has a side-effect + require('../displayUser'); + + const $ = require('jquery'); + const fetchCurrentUser = require('../fetchCurrentUser'); + + // Tell the fetchCurrentUser mock function to automatically invoke + // its callback with some data + fetchCurrentUser.mockImplementation((cb) => { + cb({ + loggedIn: true, + fullName: 'Johnny Cash', + }); + }); + + // Use jquery to emulate a click on our button + $('#button').click(); + + // Assert that the fetchCurrentUser function was called, and that the + // #username span's innter text was updated as we'd it expect. + expect(fetchCurrentUser).toBeCalled(); + expect($('#username').text()).toEqual('Johnny Cash - Logged In'); + }); +}); + Added: sandbox/trunk/src/node/jest/__tests__/fetchCurrentUser-test.js =================================================================== --- sandbox/trunk/src/node/jest/__tests__/fetchCurrentUser-test.js (rev 0) +++ sandbox/trunk/src/node/jest/__tests__/fetchCurrentUser-test.js 2016-03-07 01:10:08 UTC (rev 1890) @@ -0,0 +1,46 @@ +// __tests__/fetchCurrentUser-test.js +'use strict'; + +jest.unmock('../fetchCurrentUser.js'); + +describe('fetchCurrentUser', () => { + it('calls into $.ajax with the correct params', () => { + const $ = require('jquery'); + const fetchCurrentUser = require('../fetchCurrentUser'); + + // Call into the function we want to test + const dummyCallback = () => {}; + fetchCurrentUser(dummyCallback); + + // Now make sure that $.ajax was properly called during the previous + // 2 lines + expect($.ajax).toBeCalledWith({ + type: 'GET', + url: 'http://example.com/currentUser', + success: jasmine.any(Function), + }); + }); + + it('calls the callback when $.ajax requests are finished', () => { + const $ = require('jquery'); + const fetchCurrentUser = require('../fetchCurrentUser'); + + // Create a mock function for our callback + const callback = jest.fn(); + fetchCurrentUser(callback); + + // Now we emulate the process by which `$.ajax` would execute its own + // callback + $.ajax.mock.calls[0/*first call*/][0/*first argument*/].success({ + firstName: 'Bobby', + lastName: '");DROP TABLE Users;--', + }); + + // And finally we assert that this emulated call by `$.ajax` incurred a + // call back into the mock function we provided as a callback + expect(callback.mock.calls[0/*first call*/][0/*first arg*/]).toEqual({ + loggedIn: true, + fullName: 'Bobby ");DROP TABLE Users;--', + }); + }); +}); Added: sandbox/trunk/src/node/jest/__tests__/sum-test.js =================================================================== --- sandbox/trunk/src/node/jest/__tests__/sum-test.js (rev 0) +++ sandbox/trunk/src/node/jest/__tests__/sum-test.js 2016-03-07 01:10:08 UTC (rev 1890) @@ -0,0 +1,9 @@ +jest.unmock('../sum'); // unmock to use the actual implementation of sum + +describe('sum', () => { + it('adds 1 + 2 to equal 3', () => { + const sum = require('../sum'); + expect(sum(1, 2)).toBe(3); + }); +}); + Added: sandbox/trunk/src/node/jest/displayUser.js =================================================================== --- sandbox/trunk/src/node/jest/displayUser.js (rev 0) +++ sandbox/trunk/src/node/jest/displayUser.js 2016-03-07 01:10:08 UTC (rev 1890) @@ -0,0 +1,13 @@ +// displayUser.js +'use strict'; + +const $ = require('jquery'); +const fetchCurrentUser = require('./fetchCurrentUser.js'); + +$('#button').click(() => { + fetchCurrentUser(user => { + const loggedText = 'Logged ' + (user.loggedIn ? 'In' : 'Out'); + $('#username').text(user.fullName + ' - ' + loggedText); + }); +}); + Added: sandbox/trunk/src/node/jest/fetchCurrentUser.js =================================================================== --- sandbox/trunk/src/node/jest/fetchCurrentUser.js (rev 0) +++ sandbox/trunk/src/node/jest/fetchCurrentUser.js 2016-03-07 01:10:08 UTC (rev 1890) @@ -0,0 +1,21 @@ +// fetchCurrentUser.js +'use strict'; + +const $ = require('jquery'); + +function parseJSON(user) { + return { + loggedIn: true, + fullName: user.firstName + ' ' + user.lastName, + }; +} + +function fetchCurrentUser(callback) { + return $.ajax({ + type: 'GET', + url: 'http://example.com/currentUser', + success: user => callback(parseJSON(user)), + }); +} + +module.exports = fetchCurrentUser; Added: sandbox/trunk/src/node/jest/package.json =================================================================== --- sandbox/trunk/src/node/jest/package.json (rev 0) +++ sandbox/trunk/src/node/jest/package.json 2016-03-07 01:10:08 UTC (rev 1890) @@ -0,0 +1,16 @@ +{ + "name": "jest-test", + "version": "1.0.0", + "description": "", + "main": "sum.js", + "dependencies": { + "jest-cli": "^0.9.0", + "jquery": "^2.2.1" + }, + "devDependencies": {}, + "scripts": { + "test": "jest" + }, + "author": "", + "license": "ISC" +} Added: sandbox/trunk/src/node/jest/sum.js =================================================================== --- sandbox/trunk/src/node/jest/sum.js (rev 0) +++ sandbox/trunk/src/node/jest/sum.js 2016-03-07 01:10:08 UTC (rev 1890) @@ -0,0 +1,4 @@ +function sum(a, b) { + return a + b; +} +module.exports = sum; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |