Skip to content

Commit

Permalink
test: add a test case for joepie91 use case
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Jan 31, 2017
1 parent 713d372 commit a54aa39
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"flow-runtime": "^0.2.1",
"nearley": "^2.7.11",
"regex-parser": "^2.2.5",
"scalpel": "^2.1.0"
"scalpel": "^2.1.0",
"sprintf-js": "^1.0.3"
},
"description": "DOM extraction expression evaluator.",
"devDependencies": {
Expand Down
97 changes: 97 additions & 0 deletions test/draper/queries/joepie91.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// @flow

import test from 'ava';
import {
sprintf
} from 'sprintf-js';
import surgeon from '../../../src';
import type {
DenormalizedQueryType
} from '../../../src/types';

test('extracts a single value (expression string)', (t): void => {
let lastSelector;

const x = surgeon({
subroutines: {
captureSelector: (evaluator, selector) => {
lastSelector = selector;

return selector;
},
index: (evaluator, element) => {
// $FlowFixMe
return element.index() + 1;
},
rootSelect: (evaluator, selector) => {
// $FlowFixMe
const matches = lastSelector.find(selector);

if (matches.length !== 1) {
throw new Error('Unexpected result count.');
}

return matches;
},
sprintf: (evaluator, value, [format]) => {
return sprintf(format, value);
}
}
});

const subject = `
<h1>My shiny news site</h1>
<div class="left">
<ul>
<li><strong>Title</strong></li>
<li><a href="...">News article one</a></li>
<li><a href="...">News article two</a></li>
<li><a href="...">News article three</a></li>
</ul>
</div>
<div class="right">
<ul>
<li><strong>Date</strong></li>
<li>14 minutes ago</li>
<li>17 minutes ago</li>
<li>31 minutes ago</li>
</ul>
</div>
`;

const query: DenormalizedQueryType = [
'captureSelector',
'select .left',
{
articles: [
'select li:not(:first-child) {0,}',
{
date: 'index | sprintf ".right ul li:nth-child(%d)" | rootSelect | read property textContent',
title: 'select a | read property textContent'
}
]
}
];
const result = x(query, subject);

const expectedResult = {
articles: [
{
date: '14 minutes ago',
title: 'News article one'
},
{
date: '17 minutes ago',
title: 'News article two'
},
{
date: '31 minutes ago',
title: 'News article three'
}
]
};

t.deepEqual(result, expectedResult);
});

0 comments on commit a54aa39

Please sign in to comment.