|
| 1 | +import 'babel-polyfill' |
| 2 | +import $ from 'jquery' |
| 3 | +import 'jasmine-jquery' |
| 4 | +import 'jasmine-ajax' |
| 5 | +import { ok200, notFound404 } from './__helpers__/GithubResponses' |
| 6 | +import { GitHub } from '../src/js/modules/Github' |
| 7 | + |
| 8 | +jasmine.getFixtures().fixturesPath = 'base/src/modules' |
| 9 | + |
| 10 | +describe('Github.js - instantiation', () => { |
| 11 | + let github = null |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + github = new GitHub('jfusco') |
| 15 | + }) |
| 16 | + |
| 17 | + afterEach(() => { |
| 18 | + github = null |
| 19 | + }) |
| 20 | + |
| 21 | + it('exists in DOM', () => { |
| 22 | + loadFixtures('github.html') |
| 23 | + |
| 24 | + expect($('.github')).toBeInDOM() |
| 25 | + }) |
| 26 | + |
| 27 | + it('should error if no username is passed in', () => { |
| 28 | + // Test undefined |
| 29 | + expect((() => { |
| 30 | + new GitHub() |
| 31 | + }) |
| 32 | + ).toThrowError(Error) |
| 33 | + |
| 34 | + // Test empty string |
| 35 | + expect((() => { |
| 36 | + new GitHub('') |
| 37 | + }) |
| 38 | + ).toThrowError(Error) |
| 39 | + }) |
| 40 | + |
| 41 | + it('should set this.el to jQuery object', () => { |
| 42 | + expect(github.el instanceof $).toBe(true) |
| 43 | + }) |
| 44 | + |
| 45 | + it('should set a username', () => { |
| 46 | + expect(github.username).toBe('jfusco') |
| 47 | + }) |
| 48 | +}) |
| 49 | + |
| 50 | +describe('Github.js - ajax', () => { |
| 51 | + const github = new GitHub('DennisMartinez') |
| 52 | + |
| 53 | + let request |
| 54 | + |
| 55 | + beforeEach(done => { |
| 56 | + jasmine.Ajax.install() |
| 57 | + |
| 58 | + github.getUserData() |
| 59 | + |
| 60 | + request = jasmine.Ajax.requests.mostRecent() |
| 61 | + request.respondWith(ok200) |
| 62 | + |
| 63 | + done() |
| 64 | + }); |
| 65 | + |
| 66 | + afterEach(() => { |
| 67 | + jasmine.Ajax.uninstall(); |
| 68 | + }) |
| 69 | + |
| 70 | + it('sends the request to the right end point', () => { |
| 71 | + expect(request.url).toBe('https://api.github.com/users/DennisMartinez') |
| 72 | + }); |
| 73 | + |
| 74 | + it('uses the correct method', () => { |
| 75 | + expect(request.method).toBe('GET') |
| 76 | + }); |
| 77 | + |
| 78 | + it('should return the correct data', () => { |
| 79 | + const data = JSON.parse(request.responseText) |
| 80 | + |
| 81 | + expect(typeof data).toBe('object') |
| 82 | + expect(request.status).toBe(200) |
| 83 | + expect(data.name).toBe('Dennis Martinez') |
| 84 | + expect(data.company).toBe('Verndale') |
| 85 | + }) |
| 86 | +}) |
| 87 | + |
| 88 | +describe('Github.js - render', () => { |
| 89 | + let github = null, |
| 90 | + gitHubAvatar, |
| 91 | + gitHubInfo |
| 92 | + |
| 93 | + const data = JSON.parse(ok200.responseText) |
| 94 | + |
| 95 | + beforeEach(() => { |
| 96 | + loadFixtures('github.html') |
| 97 | + |
| 98 | + github = new GitHub('DennisMartinez') |
| 99 | + github.render(data) |
| 100 | + |
| 101 | + gitHubAvatar = $('.github-avatar') |
| 102 | + gitHubInfo = $('.github-info') |
| 103 | + }) |
| 104 | + |
| 105 | + afterEach(() => { |
| 106 | + github = null |
| 107 | + gitHubAvatar = null |
| 108 | + gitHubInfo = null |
| 109 | + }) |
| 110 | + |
| 111 | + describe('avatar', () => { |
| 112 | + it('should exist', () => { |
| 113 | + expect(gitHubAvatar).toBeInDOM() |
| 114 | + }) |
| 115 | + |
| 116 | + it('should render avatar attributes', () => { |
| 117 | + const $avatar = gitHubAvatar.find('img') |
| 118 | + |
| 119 | + expect($avatar.attr('src')).toBe(data.avatar_url) |
| 120 | + expect($avatar.attr('alt')).toBe(data.name) |
| 121 | + }) |
| 122 | + }) |
| 123 | + |
| 124 | + describe('info', () => { |
| 125 | + it('should exist', () => { |
| 126 | + expect(gitHubInfo).toBeInDOM() |
| 127 | + }) |
| 128 | + |
| 129 | + it('should render user info', () => { |
| 130 | + const $gitHubLink = gitHubInfo.find('a') |
| 131 | + const $company = gitHubInfo.find('li:nth-child(2)') |
| 132 | + const $email = gitHubInfo.find('li:nth-child(3)') |
| 133 | + const $bio = gitHubInfo.find('p') |
| 134 | + |
| 135 | + expect($gitHubLink.attr('href')).toBe(data.html_url) |
| 136 | + expect($gitHubLink.text()).toBe(data.html_url) |
| 137 | + |
| 138 | + expect($company.text()).toBe(`Company: ${data.company}`) |
| 139 | + expect($email.text()).toBe(`Email: ${data.email}`) |
| 140 | + expect($bio.text()).toBe(data.bio) |
| 141 | + }) |
| 142 | + }) |
| 143 | +}) |
0 commit comments