Skip to content

Commit 250cfe8

Browse files
committed
tests for contractor filters
1 parent 2a604bb commit 250cfe8

File tree

6 files changed

+85
-16
lines changed

6 files changed

+85
-16
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module.exports = {
99
},
1010
globals: {
1111
enz: true,
12+
xhr_calls: true,
1213
},
1314
plugins: [
1415
'react'

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ window.socket = function (public_key, config) {
154154
} else {
155155
router.history.push(url_generator(path))
156156
}
157-
}
157+
},
158+
config: config,
158159
}
159160
}

src/tests/App.test.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import React from 'react'
22
import {BrowserRouter as Router} from 'react-router-dom'
33
import App from '../components/App'
4-
import {tick} from './utils'
4+
import { xhr_setup, tick } from './utils'
5+
6+
beforeEach(() => {
7+
xhr_setup()
8+
})
59

610
it('shows tutors', async () => {
7-
// expect.assertions(2)
811
const config = {
912
router_mode: 'history',
1013
api_root: 'https://socket.tutorcruncher.com',
@@ -15,6 +18,34 @@ it('shows tutors', async () => {
1518
await tick()
1619
wrapper.update()
1720
// console.log(pretty_html(wrapper.html()))
18-
expect(global.XMLHttpRequest.mock.calls.length).toBe(2)
21+
expect(xhr_calls.length).toBe(2)
22+
expect(xhr_calls[1]).toEqual({
23+
method: 'GET',
24+
url: 'https://socket.tutorcruncher.com/good/contractors',
25+
args: null
26+
})
27+
expect(wrapper.find('.tcs-col').length).toBe(2)
28+
})
29+
30+
it('with con filter', async () => {
31+
const config = {
32+
router_mode: 'history',
33+
api_root: 'https://socket.tutorcruncher.com',
34+
mode: 'grid',
35+
contractor_filter: {
36+
label: ['foobar'],
37+
label_exclude: ['spam'],
38+
},
39+
event_callback: () => null,
40+
}
41+
const wrapper = enz.mount(<Router><App config={config} public_key={'good'} url_generator={u => u}/></Router>)
42+
await tick()
43+
wrapper.update()
44+
expect(xhr_calls.length).toBe(2)
45+
expect(xhr_calls[1]).toEqual({
46+
method: 'GET',
47+
url: 'https://socket.tutorcruncher.com/good/contractors',
48+
args: 'label=foobar&label_exclude=spam'
49+
})
1950
expect(wrapper.find('.tcs-col').length).toBe(2)
2051
})

src/tests/index.test.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,37 @@
11
import '../index'
2+
import { xhr_setup } from './utils'
3+
4+
beforeEach(() => {
5+
xhr_setup()
6+
})
27

38
it('renders grid', () => {
49
const div = document.createElement('div')
510
div.setAttribute('id', 'socket')
611
document.body.appendChild(div)
7-
window.localStorage = {}
812
const r = window.socket('good')
913
expect(r.goto).toBeTruthy()
14+
expect(r.config.contractor_filter).toEqual({})
1015
expect(div.querySelectorAll('.tcs-grid').length).toBe(1)
1116
// console.log(pretty_html(div.innerHTML))
1217
})
18+
19+
20+
it('converts contractor filter', () => {
21+
const div = document.createElement('div')
22+
div.setAttribute('id', 'socket')
23+
document.body.appendChild(div)
24+
const r = window.socket('good', {
25+
// router_mode: 'history',
26+
mode: 'grid',
27+
labels_include: ['foobar'],
28+
labels_exclude: ['spam'],
29+
event_callback: () => null,
30+
})
31+
expect(r.goto).toBeTruthy()
32+
expect(r.config.contractor_filter).toEqual({
33+
'label': ['foobar'],
34+
'label_exclude': ['spam'],
35+
})
36+
// console.log(pretty_html(div.innerHTML))
37+
})

src/tests/setup.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ import {configure} from 'enzyme'
33
import Adapter from 'enzyme-adapter-react-16'
44
import {shallow, mount, render} from 'enzyme'
55
import pretty from 'pretty'
6-
import {MockXMLHttpRequest} from './utils'
7-
86

97
configure({
108
adapter: new Adapter()
119
})
12-
global.XMLHttpRequest = jest.fn(MockXMLHttpRequest)
10+
window.localStorage = {}
1311
global.pretty_html = pretty
1412
global.enz = {
1513
shallow: shallow,

src/tests/utils.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
const RESPONSES = {
2-
'GET:https://socket.tutorcruncher.com/good/subjects': {
2+
'GET:https://socket.tutorcruncher.com/good/subjects': () => ({
33
status: 200,
44
content: JSON.stringify([
55
{'id': 29, 'name': 'English Language', 'category': 'English', 'link': '29-english-language'},
66
{'id': 31, 'name': 'English Literature', 'category': 'English', 'link': '31-english-literature'},
77
{'id': 61, 'name': 'Chinese', 'category': 'Languages', 'link': '61-chinese'}
88
])
9-
},
10-
'GET:https://socket.tutorcruncher.com/good/contractors': {
9+
}),
10+
'GET:https://socket.tutorcruncher.com/good/contractors': () => ({
1111
status: 200,
1212
content: JSON.stringify(
1313
[
@@ -36,7 +36,7 @@ const RESPONSES = {
3636
'distance': null
3737
}
3838
])
39-
}
39+
})
4040
}
4141

4242
export function MockXMLHttpRequest () {
@@ -57,9 +57,17 @@ export function MockXMLHttpRequest () {
5757
this.status = null
5858
this.statusText = null
5959
this.send = function (data) {
60-
// console.log(`XHR ${_method}: ${_url}`)
61-
const response = RESPONSES[`${_method}:${_url}`]
62-
if (response) {
60+
let args = null
61+
if (_url.includes('?')) {
62+
args = _url.substr(_url.indexOf('?') + 1, _url.length)
63+
_url = _url.substr(0, _url.indexOf('?'))
64+
}
65+
// console.log(`XHR ${_method}: ${_url} args=${args}`)
66+
const f = RESPONSES[`${_method}:${_url}`]
67+
const req = {method: _method, url: _url, args}
68+
global.xhr_calls.push(req)
69+
if (f) {
70+
const response = f(req)
6371
this.status = response.status
6472
this.responseText = response.content
6573
this.onload && this.onload()
@@ -70,5 +78,10 @@ export function MockXMLHttpRequest () {
7078
}
7179

7280
export function tick () {
73-
return new Promise(resolve => setTimeout(resolve, 0.01))
81+
return new Promise(resolve => setTimeout(resolve, 0))
82+
}
83+
84+
export function xhr_setup () {
85+
global.xhr_calls = []
86+
global.XMLHttpRequest = MockXMLHttpRequest
7487
}

0 commit comments

Comments
 (0)