Commit da6eb0cf authored by Nicolò Maria Mezzopera's avatar Nicolò Maria Mezzopera Committed by Illya Klymov

Add new matcher for interpolated strings

- matcher
- matchers test
parent b87b02f6
......@@ -35,4 +35,37 @@ export default {
message: () => message,
};
},
toMatchInterpolatedText(received, match) {
let clearReceived;
let clearMatch;
try {
clearReceived = received
.replace(/\s\s+/gm, ' ')
.replace(/\s\./gm, '.')
.trim();
} catch (e) {
return { actual: received, message: 'The received value is not a string', pass: false };
}
try {
clearMatch = match.replace(/%{\w+}/gm, '').trim();
} catch (e) {
return { message: 'The comparator value is not a string', pass: false };
}
const pass = clearReceived === clearMatch;
const message = pass
? () => `
\n\n
Expected: ${this.utils.printExpected(clearReceived)}
To not equal: ${this.utils.printReceived(clearMatch)}
`
: () =>
`
\n\n
Expected: ${this.utils.printExpected(clearReceived)}
To equal: ${this.utils.printReceived(clearMatch)}
`;
return { actual: received, message, pass };
},
};
describe('Custom jest matchers', () => {
describe('toMatchInterpolatedText', () => {
describe('malformed input', () => {
it.each([null, 1, Symbol, Array, Object])(
'fails graciously if the expected value is %s',
expected => {
expect(expected).not.toMatchInterpolatedText('null');
},
);
});
describe('malformed matcher', () => {
it.each([null, 1, Symbol, Array, Object])(
'fails graciously if the matcher is %s',
matcher => {
expect('null').not.toMatchInterpolatedText(matcher);
},
);
});
describe('positive assertion', () => {
it.each`
htmlString | templateString
${'foo'} | ${'foo'}
${'foo'} | ${'foo%{foo}'}
${'foo '} | ${'foo'}
${'foo '} | ${'foo%{foo}'}
${'foo . '} | ${'foo%{foo}.'}
${'foo bar . '} | ${'foo%{foo} bar.'}
${'foo\n\nbar . '} | ${'foo%{foo} bar.'}
${'foo bar . .'} | ${'foo%{fooStart} bar.%{fooEnd}.'}
`('$htmlString equals $templateString', ({ htmlString, templateString }) => {
expect(htmlString).toMatchInterpolatedText(templateString);
});
});
describe('negative assertion', () => {
it.each`
htmlString | templateString
${'foo'} | ${'bar'}
${'foo'} | ${'bar%{foo}'}
${'foo'} | ${'@{lol}foo%{foo}'}
${' fo o '} | ${'foo'}
`('$htmlString does not equal $templateString', ({ htmlString, templateString }) => {
expect(htmlString).not.toMatchInterpolatedText(templateString);
});
});
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment