Skip to content

Commit da123c6

Browse files
authored
Merge pull request #11 from tutorcruncher/label-filter
adding labels filter
2 parents 4782971 + 250cfe8 commit da123c6

File tree

10 files changed

+109
-22
lines changed

10 files changed

+109
-22
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'

public/index.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@
3030
</main>
3131
</body>
3232
<script>
33-
var public_key = '9c79f14df986a1ec693c'
34-
var api_root = null // 'http://localhost:8000'
33+
var public_key = '9c79f14df986a1ec693c' // '61e9d8d03109e44d7c67'
34+
var api_root = null // 'https://socket-beta.tutorcruncher.com' 'http://localhost:8000'
3535
var socket1 = socket(public_key, {
3636
element: '#socket1',
3737
router_mode: 'history',
3838
api_root: api_root,
3939
mode: 'grid',
40+
labels_include: [],
41+
labels_exclude: [],
4042
event_callback: function (name, v) {
4143
console.log(name, v)
4244
}

src/components/contractors/Contractors.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ class Contractors extends Component {
4949

5050
this.setState({selected_subject})
5151
const sub_id = selected_subject && selected_subject.id
52-
const contractors = await this.props.root.requests.get('contractors', {subject: sub_id || null})
52+
const args = Object.assign({}, this.props.config.contractor_filter, {subject: sub_id || null})
53+
const contractors = await this.props.root.requests.get('contractors', args)
5354
this.props.config.event_callback('updated_contractors', contractors)
5455
this.setState({
5556
contractors,

src/components/enquiry/EnquiryButton.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import EnquiryModal from './EnquiryModal'
44

55
const EnquiryButton = ({root, config}) => (
66
<div className="tcs-app">
7-
<div class="tcs-enquiry-modal">
7+
<div className="tcs-enquiry-modal">
88
<Link to={root.url('enquiry')} className="tcs-enquiry-button">
99
{root.get_text('enquiry_button')}
1010
</Link>

src/index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,16 @@ window.socket = function (public_key, config) {
9797
config.element = '#socket'
9898
}
9999

100+
config.contractor_filter = {}
101+
if (config.labels_include) {
102+
config.contractor_filter.label = config.labels_include
103+
delete config.labels_include
104+
}
105+
if (config.labels_exclude) {
106+
config.contractor_filter.label_exclude = config.labels_exclude
107+
delete config.labels_exclude
108+
}
109+
100110
if (config.subject_filter === undefined) {
101111
config.subject_filter = true
102112
}
@@ -144,6 +154,7 @@ window.socket = function (public_key, config) {
144154
} else {
145155
router.history.push(url_generator(path))
146156
}
147-
}
157+
},
158+
config: config,
148159
}
149160
}

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
}

src/utils.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,14 @@ export const requests = {
128128
get: async (app, path, args) => {
129129
if (args) {
130130
const arg_list = []
131+
const add_arg = (n, v) => arg_list.push(encodeURIComponent(n) + '=' + encodeURIComponent(v))
131132
for (let [name, value] of Object.entries(args)) {
132-
if (value !== null) {
133-
arg_list.push(encodeURIComponent(name) + '=' + encodeURIComponent(value))
133+
if (Array.isArray(value)) {
134+
for (let value_ of value) {
135+
add_arg(name, value_)
136+
}
137+
} else if (value !== null && value !== undefined) {
138+
add_arg(name, value)
134139
}
135140
}
136141
if (arg_list.length > 0) {

0 commit comments

Comments
 (0)