insomnia/packages/insomnia-xpath/__tests__/index.test.js

37 lines
880 B
JavaScript
Raw Normal View History

2018-06-25 17:42:50 +00:00
const { query } = require('..');
2018-06-25 17:42:50 +00:00
function q(description, args, result) {
it(description, () => {
expect(query(...args)).toEqual(result);
});
}
describe('query()', () => {
2018-06-25 17:42:50 +00:00
q('handles missing query', ['<foo><bar></bar></foo>'], []);
2018-06-25 17:42:50 +00:00
q(
'handles basic query',
['<x><y>foo</y><y>bar</y></x>', '//y'],
[
{ inner: 'foo', outer: '<y>foo</y>' },
{ inner: 'bar', outer: '<y>bar</y>' },
],
);
2018-06-25 17:42:50 +00:00
q(
'handles attribute query',
['<x><y foo="bar">foo</y><y hi="there">bar</y></x>', '//*[@foo="bar"]'],
[{ inner: 'foo', outer: '<y foo="bar">foo</y>' }],
);
2018-06-25 17:42:50 +00:00
q(
'handles string query',
['<x><y>foo</y><y>bar</y></x>', 'substring(//y[1], 2)'],
[{ inner: 'oo', outer: 'oo' }],
);
it('handles invalid query', () => {
2018-10-17 16:42:33 +00:00
expect(() => query('<hi>there</hi>', '//[]')).toThrowError('Invalid XPath query: //[]');
});
});