Skip to content

Commit

Permalink
Merge pull request #5 from capJavert/feature/linter
Browse files Browse the repository at this point in the history
Add linter and tests
  • Loading branch information
capJavert authored Aug 25, 2019
2 parents acc2b85 + f08a147 commit 4848ed9
Show file tree
Hide file tree
Showing 27 changed files with 4,025 additions and 272 deletions.
7 changes: 7 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
src/assets/js/agent.js
src/assets/js/clippy.js
src/assets/js/jquery.min.js
src/assets/js/web-storage-object.js
29 changes: 29 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"env": {
"browser": true,
"es6": true,
"jest": true
},
"extends": [
"airbnb-base"
],
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly",
"browser": true,
"chrome": "readonly",
"clippy": "readonly",
"webStorageObject": "readonly"
},
"parserOptions": {
"ecmaVersion": 2018
},
"rules": {
"no-console": "off",
"global-require": "off",
"semi": "off",
"comma-dangle": "off",
"import/no-dynamic-require": "off",
"indent": ["error", 4]
}
}
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10.3.0
11 changes: 11 additions & 0 deletions __tests__/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../.eslintrc",
"globals": {
"jest": true,
"clippyController": "readonly"
},
"rules": {
"no-debugger": "off",
"no-console": "off"
}
}
208 changes: 208 additions & 0 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
const createBrowser = () => {
browser = {
runtime: {
sendMessage() {},
onMessage: {
addListener(listener) {
this.listener = listener
}
}
}
}
}

const dictionary = {
localhost: 'It works! Good job!',
twitter: 'Tweets can only be 280 characters long!',
github: 'Need lessons in Python?',
quora: [
'????????????????',
'What is my purpose?'
]
}

const createAgent = () => ({
isActive: false,
play(animation, length, callback) {
this.animation = animation
this.animationLength = length
this.callback = callback

if (typeof this.callback === 'function') {
this.callback()
}
},
stop() {},
show() {
this.isActive = true
},
hide() {
this.isActive = false
},
speak(comment) {
this.comment = comment
}
})

beforeEach(() => {
createBrowser()
require('../src/index')
})

afterEach(() => {
jest.resetModules()
})

test('agent init', () => {
const agent = createAgent()
clippyController.init(agent)

expect(clippyController.agent).not.toBeNull()
})

it('should check active status on init', () => {
browser.runtime.sendMessage = (message, callback) => {
if (message.name === 'isActive') {
callback({ value: true })
}
}

const agent = createAgent()
clippyController.init(agent)

expect(clippyController.agent.isActive).toBe(true)
})

it('should respect off switch on init', () => {
browser.runtime.sendMessage = (message, callback) => {
if (message.name === 'isActive') {
callback({ value: false })
}
}

const agent = createAgent()
clippyController.init(agent)

expect(clippyController.agent.isActive).toBe(false)
})

it('should prefetch comments on init', () => {
expect.assertions(2)
let commentsMessage
browser.runtime.sendMessage = (message) => {
if (message.name === 'comments') {
commentsMessage = message
browser.runtime.onMessage.listener({ name: 'comments', value: dictionary })
}
}

const agent = createAgent()
clippyController.init(agent)

expect(commentsMessage).toEqual({ name: 'comments' })
expect(clippyController.comments).toEqual(dictionary)
})

it('should talk', () => {
delete global.window.location
global.window.location = {
hostname: 'github'
}

const agent = createAgent()
clippyController.init(agent)
clippyController.comments = dictionary
clippyController.talk()

expect(clippyController.lastComment).toEqual(dictionary.github)
})

test('pick random comment', () => {
delete global.window.location
global.window.location = {
hostname: 'quora'
}

const agent = createAgent()
clippyController.init(agent)
clippyController.comments = dictionary
clippyController.talk()

expect(dictionary.quora).toContain(clippyController.lastComment)
})

it('should send idle message', () => {
let idleMessage
browser.runtime.sendMessage = (message) => {
if (message.name === 'idle') {
idleMessage = message
}
}

clippyController.idle()

expect(idleMessage).toEqual({ name: 'idle' })
})

test('animation trigger', () => {
expect.assertions(3)
const animations = ['Congratulate', 'LookRight', 'SendMail', 'Thinking']

const agent = createAgent()
clippyController.init(agent)
clippyController.animations = animations
clippyController.animate(() => {})

expect(clippyController.animations).toContain(agent.animation)
expect(agent.animationLength).toEqual(5000)
expect(agent.callback).toBeDefined()
})

test('isActive listener', () => {
expect.assertions(2)

const agent = createAgent()
clippyController.init(agent)

browser.runtime.onMessage.listener({ name: 'isActive', value: true })

expect(agent.isActive).toBe(true)

browser.runtime.onMessage.listener({ name: 'isActive', value: false })

expect(agent.isActive).toBe(false)
})

test('comments listener', () => {
const agent = createAgent()
clippyController.init(agent)

browser.runtime.onMessage.listener({ name: 'comments', value: dictionary })

expect(clippyController.comments).toEqual(dictionary)
})

test('animate listener', () => {
expect.assertions(2)

const agent = createAgent()
clippyController.init(agent)

browser.runtime.sendMessage = (message, callback) => {
if (message.name === 'isActive') {
callback({ value: false })
}
}
browser.runtime.onMessage.listener({ name: 'animate' })

expect(agent.animation).toBeUndefined()

browser.runtime.sendMessage = (message, callback) => {
if (message.name === 'isActive') {
callback({ value: true })
}
}
browser.runtime.onMessage.listener({ name: 'animate' })

expect(agent.animation).toBeDefined()
})
Loading

0 comments on commit 4848ed9

Please sign in to comment.