|
| 1 | +/* global jest, describe, beforeEach, it, expect, spyOn */ |
| 2 | + |
| 3 | +jest.dontMock('reflux'); |
| 4 | +jest.dontMock('../../actions/actions.js'); |
| 5 | +jest.dontMock('../../utils/api-requests'); |
| 6 | +jest.dontMock('../../components/login.js'); |
| 7 | +jest.dontMock('../../stores/auth.js'); |
| 8 | + |
| 9 | +var React = require('react/addons'); |
| 10 | +var TestUtils = React.addons.TestUtils; |
| 11 | + |
| 12 | +describe('Test for Login Component', function () { |
| 13 | + |
| 14 | + var apiRequests, Actions, Login, AuthStore, Router; |
| 15 | + |
| 16 | + beforeEach(function () { |
| 17 | + // Mock Electron's window.require |
| 18 | + // and remote.ipc |
| 19 | + window.require = function () { |
| 20 | + return { |
| 21 | + require: function () { |
| 22 | + return; |
| 23 | + }, |
| 24 | + sendChannel: function () { |
| 25 | + return; |
| 26 | + } |
| 27 | + }; |
| 28 | + }; |
| 29 | + |
| 30 | + // Mock localStorage |
| 31 | + window.localStorage = { |
| 32 | + item: false, |
| 33 | + getItem: function () { |
| 34 | + return this.item; |
| 35 | + } |
| 36 | + }; |
| 37 | + |
| 38 | + apiRequests = require('../../utils/api-requests.js'); |
| 39 | + Actions = require('../../actions/actions.js'); |
| 40 | + AuthStore = require('../../stores/auth.js'); |
| 41 | + Login = require('../../components/login.js'); |
| 42 | + Router = require('react-router'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('Should get the token from github', function () { |
| 46 | + |
| 47 | + AuthStore.authStatus = function () { |
| 48 | + return false; |
| 49 | + }; |
| 50 | + |
| 51 | + var instance; |
| 52 | + React.withContext({router: new Router()}, function () { |
| 53 | + instance = TestUtils.renderIntoDocument(<Login />); |
| 54 | + }); |
| 55 | + |
| 56 | + expect(instance.authGithub).toBeDefined(); |
| 57 | + expect(instance.requestGithubToken).toBeDefined(); |
| 58 | + |
| 59 | + var superagent = require('superagent'); |
| 60 | + superagent.__setResponse(200, 'ok', {'access_token': '123123123'}, false); |
| 61 | + |
| 62 | + instance.requestGithubToken({}, '456456'); |
| 63 | + |
| 64 | + }); |
| 65 | + |
| 66 | + it('Should fail to get the token from github', function () { |
| 67 | + |
| 68 | + AuthStore.authStatus = function () { |
| 69 | + return false; |
| 70 | + }; |
| 71 | + |
| 72 | + var instance; |
| 73 | + React.withContext({router: new Router()}, function () { |
| 74 | + instance = TestUtils.renderIntoDocument(<Login />); |
| 75 | + }); |
| 76 | + |
| 77 | + expect(instance.authGithub).toBeDefined(); |
| 78 | + expect(instance.requestGithubToken).toBeDefined(); |
| 79 | + |
| 80 | + var superagent = require('superagent'); |
| 81 | + superagent.__setResponse(400, false, false, false); |
| 82 | + |
| 83 | + instance.requestGithubToken({}, '456456'); |
| 84 | + |
| 85 | + }); |
| 86 | + |
| 87 | +}); |
0 commit comments