|
| 1 | +const createBrowser = () => { |
| 2 | + browser = { |
| 3 | + runtime: { |
| 4 | + sendMessage() {}, |
| 5 | + onMessage: { |
| 6 | + addListener(listener) { |
| 7 | + this.listener = listener |
| 8 | + } |
| 9 | + } |
| 10 | + } |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +const dictionary = { |
| 15 | + localhost: 'It works! Good job!', |
| 16 | + twitter: 'Tweets can only be 280 characters long!', |
| 17 | + github: 'Need lessons in Python?', |
| 18 | + quora: [ |
| 19 | + '????????????????', |
| 20 | + 'What is my purpose?' |
| 21 | + ] |
| 22 | +} |
| 23 | + |
| 24 | +const createAgent = () => ({ |
| 25 | + isActive: false, |
| 26 | + play(animation, length, callback) { |
| 27 | + this.animation = animation |
| 28 | + this.animationLength = length |
| 29 | + this.callback = callback |
| 30 | + |
| 31 | + if (typeof this.callback === 'function') { |
| 32 | + this.callback() |
| 33 | + } |
| 34 | + }, |
| 35 | + stop() {}, |
| 36 | + show() { |
| 37 | + this.isActive = true |
| 38 | + }, |
| 39 | + hide() { |
| 40 | + this.isActive = false |
| 41 | + }, |
| 42 | + speak(comment) { |
| 43 | + this.comment = comment |
| 44 | + } |
| 45 | +}) |
| 46 | + |
| 47 | +beforeEach(() => { |
| 48 | + createBrowser() |
| 49 | + require('../src/index') |
| 50 | +}) |
| 51 | + |
| 52 | +afterEach(() => { |
| 53 | + jest.resetModules(); |
| 54 | +}) |
| 55 | + |
| 56 | +test('agent init', () => { |
| 57 | + const agent = createAgent() |
| 58 | + clippyController.init(agent) |
| 59 | + |
| 60 | + expect(clippyController.agent).not.toBeNull() |
| 61 | +}) |
| 62 | + |
| 63 | +it('should check active status on init', () => { |
| 64 | + browser.runtime.sendMessage = (message, callback) => { |
| 65 | + if (message.name === 'isActive') { |
| 66 | + callback({ value: true }) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + const agent = createAgent() |
| 71 | + clippyController.init(agent) |
| 72 | + |
| 73 | + expect(clippyController.agent.isActive).toBe(true) |
| 74 | +}) |
| 75 | + |
| 76 | +it('should respect off switch on init', () => { |
| 77 | + browser.runtime.sendMessage = (message, callback) => { |
| 78 | + if (message.name === 'isActive') { |
| 79 | + callback({ value: false }) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + const agent = createAgent() |
| 84 | + clippyController.init(agent) |
| 85 | + |
| 86 | + expect(clippyController.agent.isActive).toBe(false) |
| 87 | +}) |
| 88 | + |
| 89 | +it('should prefetch comments on init', () => { |
| 90 | + expect.assertions(2) |
| 91 | + let commentsMessage |
| 92 | + browser.runtime.sendMessage = (message) => { |
| 93 | + if (message.name === 'comments') { |
| 94 | + commentsMessage = message |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + const agent = createAgent() |
| 99 | + clippyController.init(agent) |
| 100 | + |
| 101 | + expect(commentsMessage).toEqual({ name: 'comments' }) |
| 102 | + |
| 103 | + browser.runtime.onMessage.listener({ name: 'comments', value: dictionary }) |
| 104 | + |
| 105 | + expect(clippyController.comments).toEqual(dictionary) |
| 106 | +}) |
| 107 | + |
| 108 | +it('should talk', () => { |
| 109 | + delete global.window.location |
| 110 | + global.window.location = { |
| 111 | + hostname: 'github' |
| 112 | + } |
| 113 | + |
| 114 | + const agent = createAgent() |
| 115 | + clippyController.init(agent) |
| 116 | + clippyController.comments = dictionary |
| 117 | + clippyController.talk() |
| 118 | + |
| 119 | + expect(clippyController.lastComment).toEqual(dictionary.github) |
| 120 | +}) |
| 121 | + |
| 122 | +test('pick random comment', () => { |
| 123 | + delete global.window.location |
| 124 | + global.window.location = { |
| 125 | + hostname: 'quora' |
| 126 | + } |
| 127 | + |
| 128 | + const agent = createAgent() |
| 129 | + clippyController.init(agent) |
| 130 | + clippyController.comments = dictionary |
| 131 | + clippyController.talk() |
| 132 | + |
| 133 | + expect(dictionary.quora).toContain(clippyController.lastComment) |
| 134 | +}) |
| 135 | + |
| 136 | +it('should send idle message', () => { |
| 137 | + let idleMessage |
| 138 | + browser.runtime.sendMessage = (message) => { |
| 139 | + if (message.name === 'idle') { |
| 140 | + idleMessage = message |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + clippyController.idle() |
| 145 | + |
| 146 | + expect(idleMessage).toEqual({ name: 'idle' }) |
| 147 | +}) |
| 148 | + |
| 149 | +test('animation trigger', () => { |
| 150 | + expect.assertions(3) |
| 151 | + const animations = ['Congratulate', 'LookRight', 'SendMail', 'Thinking'] |
| 152 | + |
| 153 | + const agent = createAgent() |
| 154 | + clippyController.init(agent) |
| 155 | + clippyController.animations = animations |
| 156 | + clippyController.animate(() => {}) |
| 157 | + |
| 158 | + expect(clippyController.animations).toContain(agent.animation) |
| 159 | + expect(agent.animationLength).toEqual(5000) |
| 160 | + expect(agent.callback).toBeDefined() |
| 161 | +}) |
0 commit comments