Skip to content

Commit ce2b956

Browse files
author
vvo
committed
fix(priceRanges): pass the bound refine to the form
Form was no more working. Took the opportunity to write real tests for the priceRanges widget.
1 parent e2ff425 commit ce2b956

File tree

9 files changed

+1623
-1104
lines changed

9 files changed

+1623
-1104
lines changed

functional-tests/config.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import server from './server';
1+
import startServer from './startServer';
22

33
let conf = {
44
specs: [
@@ -14,7 +14,7 @@ let conf = {
1414
baseUrl: 'http://localhost:9000',
1515
framework: 'mocha',
1616
onPrepare() {
17-
return server();
17+
return startServer();
1818
},
1919
before() {
2020
let init = browser
@@ -64,13 +64,6 @@ if (process.env.CI === 'true') {
6464
browserName: 'safari',
6565
version: '9',
6666
...defaultCapabilities
67-
}, {
68-
browserName: 'iphone',
69-
deviceName: 'iPhone 6 Plus',
70-
platform: 'OS X 10.10',
71-
version: '9.2',
72-
deviceOrientation: 'portrait',
73-
...defaultCapabilities
7467
}],
7568
...conf
7669
};
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import expect from 'expect';
2+
import {getCurrentRefinements} from '../utils.js';
3+
4+
describe('priceRanges', () => {
5+
const widget = '#price-ranges';
6+
const item = '.item';
7+
const priceRange = 'a*=$80';
8+
9+
it('has some default ranges', () =>
10+
browser
11+
.element(widget)
12+
.elements(item)
13+
.then(items =>
14+
expect(items.value.length)
15+
.toBe(7)
16+
)
17+
);
18+
19+
it('can refine by clicking a range', () =>
20+
browser
21+
.element(widget)
22+
.click(priceRange)
23+
.then(getCurrentRefinements)
24+
.then(refinements =>
25+
expect(refinements.length).toBe(2) &&
26+
expect(refinements[0].name).toBe('Price ≤ 160') &&
27+
expect(refinements[1].name).toBe('Price ≥ 80')
28+
)
29+
);
30+
31+
context('the form', () => {
32+
beforeEach(() =>
33+
browser
34+
.element(widget)
35+
.setValue('label:nth-of-type(1) input', '94')
36+
.setValue('label:nth-of-type(2) input', '113')
37+
);
38+
39+
it('can be submitted with ENTER key', () =>
40+
browser
41+
.element(widget)
42+
.addValue('label:nth-of-type(2) input', 'Enter')
43+
.then(getCurrentRefinements)
44+
.then(refinements => expect(refinements.length).toBe(2) &&
45+
expect(refinements[0].name).toBe('Price ≤ 113') &&
46+
expect(refinements[1].name).toBe('Price ≥ 94')
47+
)
48+
);
49+
50+
it('can be submitted by using the submit button', () =>
51+
browser
52+
.element(widget)
53+
.click('button')
54+
.then(getCurrentRefinements)
55+
.then(refinements => expect(refinements.length).toBe(2) &&
56+
expect(refinements[0].name).toBe('Price ≤ 113') &&
57+
expect(refinements[1].name).toBe('Price ≥ 94')
58+
)
59+
);
60+
});
61+
});
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ export default () => {
2121

2222
app.use(express.static(path.join(__dirname, 'app')));
2323
app.use(express.static(path.join(__dirname, '..', 'dist')));
24-
let server = app.listen(9000);
24+
let server = app.listen(process.env.PORT || 9000);
2525

26-
server.once('listening', resolve);
26+
server.once('listening', () => resolve(server));
2727
server.once('error', reject);
2828
});
2929
};

functional-tests/utils.js

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import cheerio from 'cheerio';
2+
13
export function getNumberOfResults() {
24
return browser
35
.getText('#stats')
@@ -6,12 +8,8 @@ export function getNumberOfResults() {
68

79
export function getCurrentRefinements() {
810
return browser
9-
.element('#current-refined-values .item')
10-
.then(() => browser
11-
.getText('#current-refined-values .item')
12-
.then(res => Array.isArray(res) ? res : [res])
13-
.then(res => res.map(formatTextRefinement))
14-
)
11+
.getHTML('#current-refined-values .facet-value > div')
12+
.then(formatRefinements)
1513
.catch(() => Promise.resolve([]));
1614
}
1715

@@ -21,6 +19,7 @@ export function clearAll() {
2119
}
2220

2321
export const searchBox = {
22+
selector: '#search-box',
2423
set(query) {
2524
return browser.setValue('#search-box', query);
2625
},
@@ -33,30 +32,32 @@ export const searchBox = {
3332
};
3433

3534
// export function getRefinementFromSelector(selector) {
36-
// return browser.getText(selector).then(formatTextRefinement);
35+
// return browser.getText(selector).then(formatRefinements);
3736
// }
3837

39-
function formatTextRefinement(textRefinement) {
40-
let data = textRefinement.split('\n');
41-
42-
// some browsers like IE will send a slighty different html
43-
// they send "Appliances 1543" instead of "Appliances \n1543"
44-
if (data.length === 1) {
45-
let countPosition = textRefinement.lastIndexOf(' ');
46-
data = [
47-
textRefinement.slice(0, countPosition),
48-
textRefinement.slice(-(textRefinement.length - countPosition))
49-
];
38+
function formatRefinements(refinementsAsHTML) {
39+
if (!Array.isArray(refinementsAsHTML)) {
40+
refinementsAsHTML = [refinementsAsHTML];
5041
}
5142

52-
const refinement = {
53-
name: data[0].trim()
54-
};
43+
return refinementsAsHTML.map(refinementAsHTML => {
44+
// element is (simplified) <div>facetName <span>facetCount</span></div>
45+
const element = cheerio.parseHTML(refinementAsHTML)[0];
5546

56-
if (data[1] !== undefined) {
57-
refinement.count = parseInt(data[1], 10);
58-
}
47+
// <div>**facetName** <span>facetCount</span></div>
48+
const name = element.firstChild.nodeValue.trim();
49+
50+
// some widgets have no counts (pricesRanges)
51+
// <div>facetName <span>**facetCount**</span></div>
52+
const maybeCount = element.firstChild.nextSibling.children;
53+
54+
return {
55+
name,
5956

60-
return refinement;
57+
count: maybeCount && maybeCount[0] && maybeCount[0].nodeValue !== undefined ?
58+
parseInt(maybeCount[0].nodeValue, 10) :
59+
'n/a'
60+
};
61+
});
6162
}
6263

0 commit comments

Comments
 (0)