Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for expect.it #297

Merged
merged 5 commits into from
Nov 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -965,8 +965,13 @@ module.exports = {
'<DOMElement> to [exhaustively] satisfy <object>',
(expect, subject, value) => {
const isHtml = isInsideHtmlDocument(subject);
ensureSupportedSpecOptions(value);

if (expect.argTypes[0].is('expect.it')) {
expect.context.thisObject = subject;
return value(subject, expect.context);
}

ensureSupportedSpecOptions(value);
const promiseByKey = {
name: expect.promise(() => {
if (value && typeof value.name !== 'undefined') {
Expand Down Expand Up @@ -1745,7 +1750,11 @@ module.exports = {
const valueType = expect.findTypeOf(value);
let spec = value;

if (valueType.is('DOMElement')) {
if (valueType.is('expect.it')) {
throw new Error(
'Unsupported value for "to contain" assertion: expect.it'
);
} else if (valueType.is('DOMElement')) {
spec = convertDOMNodeToSatisfySpec(value, isHtml);
} else if (valueType.is('string')) {
const documentFragment = isHtml
Expand Down
77 changes: 77 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2513,6 +2513,67 @@ describe('unexpected-dom', () => {
`
);
});

describe('when used with expect.it', () => {
it('succeeds if the given structure is present', () => {
expect(
parseHtmlNode('<div class="foobar"></div>'),
'to satisfy',
expect.it('to have attributes', 'class')
);
});

it('fails with a diff if the given structure is absent', () => {
expect(
() =>
expect(
parseHtmlNode('<div></div>'),
'to satisfy',
expect.it('to have attributes', 'class')
),
'to throw an error satisfying to equal snapshot',
expect.unindent`
expected <div></div> to have attributes 'class'

<div
// missing class
></div>
`
);
});

it('succeeds with a negated assertion', () => {
expect(
parseHtmlNode('<div></div>'),
'to satisfy',
expect.it('not to have attributes', 'class')
);
});

it('succeeds when used as the name option', () => {
expect(parseHtmlNode('<my-foo-bar></my-foo-bar>'), 'to satisfy', {
name: expect.it(value => value.indexOf('-').length === 2)
});
});

it('succeeds when used as the children option', () => {
expect(
parseHtmlNode(
'<div><i>Hello</i> <span class="name something-else">Jane Doe</span></div>'
),
'to satisfy',
{
children: expect.it('to have length', 3)
}
);
});

it('succeeds when used as the textContent option', () => {
expect(parseHtmlNode('<div>bar foo</div>'), 'to satisfy', {
textContent: expect.it('not to start with', 'foo')
});
});
});
});

describe('queried for', () => {
Expand Down Expand Up @@ -3510,6 +3571,22 @@ describe('unexpected-dom', () => {
});
});

describe('when used with expect.it', () => {
it('fails with an unsupported message at the top-level', () => {
expect(
() => {
expect(
parseHtmlNode('<div></div>'),
'to contain',
expect.it('not to have attributes', 'class')
);
},
'to throw',
'Unsupported value for "to contain" assertion: expect.it'
);
});
});

it('supports only stating a subset of the classes', () => {
expect(
'<div><i class="greeting">Hello</i> <span class="name something-else and-this">Jane Doe</span></div>',
Expand Down