Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Feature/unit testing #8

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{
"presets": ["env", "react-app"],
"plugins": ["transform-es2015-modules-umd"],
"ignore": ["**/*.test.js"]
}
"presets": ["env", "react"]
}
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"parserOptions": {
"sourceType": "module"
},
"plugins": ["jest"],
"env": {
"node": true,
"es6": true,
Expand All @@ -13,4 +14,4 @@
"no-extra-semi": 0,
"no-console": 0
}
}
}
7 changes: 0 additions & 7 deletions .eslintrc.js

This file was deleted.

4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules
package-lock.json
build
build
coverage
.DS_Store
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"test": "jest",
"test:watch": "jest --watch",
"test:ci": "npm t -- --coverage --json --outputFile=test-results.json",
"coverage": "npm test -- --coverage --collectCoverageFrom=src/**/*.js",
"cicoverage": "npm test",
"lint": "eslint --ignore-path .gitignore 'react-freshchat.js'",
"lint:ci": "npm run lint -- -o lint-results.json -f json",
Expand All @@ -33,20 +34,25 @@
"dependencies": {
"babel-runtime": "^6.26.0",
"lodash": "^4.17.4",
"react": "^16.2.0"
"react": "^16.2.0",
"react-dom": "^16.2.0"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-eslint": "^8.0.1",
"babel-jest": "^22.2.2",
"babel-plugin-transform-es2015-modules-umd": "^6.24.1",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.6.1",
"babel-preset-react-app": "^3.1.1",
"danger": "~0.18.0",
"duti": "latest",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"eslint": "^4.15.0",
"eslint-config-smartprocure": "^1.0.2",
"eslint-plugin-jest": "^21.2.0",
"eslint-plugin-jest": "^21.8.0",
"eslint-plugin-prettier": "^2.3.1",
"ghooks": "^2.0.0",
"jest": "^22.1.2",
Expand Down
43 changes: 43 additions & 0 deletions src/queue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import _ from 'lodash/fp'

export default class Queue {
constructor() {
this.data = []
this.index = 0
}

queue(value) {
this.data.push(value)
}

dequeue() {
if (this.index > -1 && this.index < this.data.length) {
let result = this.data[this.index++]

if (this.isEmpty) {
this.reset()
}

return result
}
}

get isEmpty() {
return this.index >= this.data.length
}

dequeueAll(cb) {
if (!_.isFunction(cb)) {
throw new Error(`Please provide a callback`)
}

while (!this.isEmpty) {
cb(this.dequeue())
}
}

reset() {
this.data.length = 0
this.index = 0
}
}
86 changes: 86 additions & 0 deletions src/queue.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// import Queue from 'queue'
let Queue = require('./queue').default

test(`queue method`, () => {
let q = new Queue()
expect(q.data).toEqual([])
expect(q.index).toBe(0)

q.queue('testing')

expect(q.data).toEqual(['testing'])
expect(q.index).toBe(0)

q.queue('asd')

expect(q.data).toEqual(['testing', 'asd'])
expect(q.index).toBe(0)
})

test(`dequeue method`, () => {
let q = new Queue()
q.queue('testing')
q.queue('asd')

expect(q.dequeue()).toBe('testing')
expect(q.data).toEqual(['testing', 'asd'])
expect(q.index).toBe(1)

expect(q.dequeue()).toBe('asd')
expect(q.data).toEqual([]) // it should be empty because we reset the queue
expect(q.index).toBe(0) // it should be 0 because we reset the queue

expect(q.dequeue()).toBe(undefined)
})

test(`isEmpty`, () => {
let q = new Queue()
expect(q.isEmpty).toBe(true)

q.queue('testing')
expect(q.isEmpty).toBe(false)

q.queue('asd')
expect(q.isEmpty).toBe(false)

q.dequeue()
expect(q.isEmpty).toBe(false)

q.dequeue()
expect(q.isEmpty).toBe(true)
})

test(`dequeueAll method`, () => {
let q = new Queue()
let cb = jest.fn()

q.queue('testing')
q.queue('asd')

expect(() => {
q.dequeueAll()
}).toThrowError('Please provide a callback')

q.dequeueAll(cb)

expect(cb).toHaveBeenCalledTimes(2)
expect(cb.mock.calls[0][0]).toBe('testing')
expect(cb.mock.calls[1][0]).toBe('asd')
})

test(`reset method`, () => {
let q = new Queue()

q.queue('testing')
q.queue('asd')
q.queue('123')

q.dequeue()

expect(q.index).toBe(1)

q.reset()

expect(q.data).toEqual([])
expect(q.index).toBe(0)
})
113 changes: 42 additions & 71 deletions src/react-freshchat.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,51 @@
import _ from 'lodash/fp'
import React from 'react'

class Queue {
constructor() {
this.data = []
this.index = 0
}

queue(value) {
this.data.push(value)
}

dequeue() {
if (this.index > -1 && this.index < this.data.length) {
let result = this.data[this.index++]

if (this.isEmpty) {
this.reset()
}

return result
}
}

get isEmpty() {
return this.index >= this.data.length
}

dequeueAll(cb) {
if (!_.isFunction(cb)) {
throw new Error(`Please provide a callback`)
}

while (!this.isEmpty) {
let { method, args } = this.dequeue()
cb(method, args)
}
}

reset() {
this.data.length = 0
this.index = 0
}
}
import Queue from './queue'

let fakeWidget
let earlyCalls = new Queue()

export let widget = (fake = fakeWidget) => {
if (window.fcWidget) return window.fcWidget
if (!fake) fake = mockMethods(availableMethods)
export let widget = (
fake = fakeWidget,
real = window.fcWidget,
methods = availableMethods
) => {
if (real) return real
if (!fake) fake = mockMethods(earlyCalls, methods)
return fake
}

let mockMethods = methods => {
export let mockMethods = (Q = earlyCalls, methods = availableMethods) => {
let obj = {}
methods.forEach(method => {
obj = _.set(method, queueMethod(method), obj)
obj = _.set(method, queueMethod(Q, method), obj)
})
return obj
}

let queueMethod = method => (...args) => {
earlyCalls.queue({ method, args })
export let queueMethod = (Q, method) => (...args) => {
Q.queue({ method, args })
}

let loadScript = () => {
export let loadScript = (document = document, widget = window.fcWidget) => {
let id = 'freshchat-lib'
if (document.getElementById(id) || window.fcWidget) return
if (widget || document.getElementById(id)) return
let script = document.createElement('script')
script.id = id
script.async = 'true'
script.type = 'text/javascript'
script.src = 'https://wchat.freshchat.com/js/widget.js'
script.id = id
document.head.appendChild(script)
}

class FreshChat extends React.Component {
constructor(props) {
super(props)


this.win = window || {}
this.doc = document || {}
this.checkAndInit = this.checkAndInit.bind(this)

let { token, ...moreProps } = props

if (!token) {
Expand All @@ -93,14 +59,19 @@ class FreshChat extends React.Component {
})
}

mutateOnInit(settings) {
let tmp = settings.onInit
settings.onInit = () => tmp(widget())
return settings
}

init(settings) {
if (settings.onInit) {
let tmp = settings.onInit
settings.onInit = () => tmp(widget())
}
let { fcWidget } = this.win

if (window.fcWidget) {
window.fcWidget.init(settings)
if (settings.onInit) this.mutateOnInit(settings)

if (fcWidget) {
fcWidget.init(settings)
if (settings.onInit) {
settings.onInit()
}
Expand All @@ -112,14 +83,18 @@ class FreshChat extends React.Component {
lazyInit(settings) {
widget().init(settings) // Can't use window.fcSettings because sometimes it doesn't work

loadScript()
loadScript(this.doc)

this.interval = setInterval(this.checkAndInit(settings), 1000)
}

let interval = setInterval(() => {
if (window.fcWidget) {
clearInterval(interval)
checkAndInit(settings) {
return () => {
if (this.win.fcWidget) {
clearInterval(this.interval)
try {
earlyCalls.dequeueAll((method, value) => {
window.fcWidget[method](...value)
earlyCalls.dequeueAll(({ method, args }) => {
this.win.fcWidget[method](...args)
})
} catch (e) {
console.error(e)
Expand All @@ -128,16 +103,12 @@ class FreshChat extends React.Component {
settings.onInit()
}
}
}, 1000)
}
}

render() {
return false
}

componentWillUnmount() {
widget().close()
}
}

let availableMethods = [
Expand Down
Loading