Skip to content

Commit fa847ad

Browse files
committed
feature(linter): added integration tests for clippy controller
1 parent 2597e08 commit fa847ad

File tree

5 files changed

+178
-2
lines changed

5 files changed

+178
-2
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"env": {
33
"browser": true,
4-
"es6": true
4+
"es6": true,
5+
"jest": true
56
},
67
"extends": [
78
"airbnb-base"

__tests__/.eslintrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "../.eslintrc",
3+
"globals": {
4+
"jest": true,
5+
"clippyController": "readonly"
6+
},
7+
"rules": {
8+
"no-debugger": "off",
9+
"no-console": "off"
10+
}
11+
}

__tests__/index.test.js

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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+
})

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Clippy MS Word Office asistant is now back to help inside your browser!",
55
"main": "src/index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1",
7+
"test": "jest",
88
"build": "web-ext build --ignore-files .nvmrc .editorconfig .eslintrc package*.json src/assets/img/screenshots",
99
"lint": "eslint --max-warnings=0 src"
1010
},
@@ -23,6 +23,7 @@
2323
"eslint-config-airbnb-base": "^14.0.0",
2424
"eslint-plugin-import": "^2.18.2",
2525
"husky": "^3.0.4",
26+
"jest": "^24.9.0",
2627
"web-ext": "^2.9.3"
2728
},
2829
"husky": {

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,5 @@ browser.runtime.onMessage.addListener((request) => {
116116
break
117117
}
118118
})
119+
120+
window.clippyController = clippyController

0 commit comments

Comments
 (0)