From ffa8068c34c014989493afa6be84d51ade8fa621 Mon Sep 17 00:00:00 2001 From: Sune Simonsen Date: Wed, 20 Nov 2019 21:40:52 +0100 Subject: [PATCH] Added an equal method to the DOMDocumentFragment type --- src/index.js | 3 + test/index.spec.js | 172 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 175 insertions(+) diff --git a/src/index.js b/src/index.js index 132ef8f9..367f5bd0 100644 --- a/src/index.js +++ b/src/index.js @@ -497,6 +497,9 @@ module.exports = { .append(inspect(documentFragment.childNodes, depth)) .text(']'); }, + equal(a, b, equal) { + return this.baseType.equal(a, b) && equal(a.childNodes, b.childNodes); + }, diff(actual, expected, output, diff, inspect, equal) { output.inline = true; output.block( diff --git a/test/index.spec.js b/test/index.spec.js index bc8b8968..233aab5e 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -1522,6 +1522,178 @@ describe('unexpected-dom', () => { }); }); + describe('to equal', () => { + describe('on HTML elements', () => { + it('should succeeds if they are equal', () => { + expect( + parseHtmlNode( + '' + ), + 'to equal', + parseHtmlNode( + '' + ) + ); + }); + + it('should fail if they are not equal', () => { + expect( + () => { + expect( + parseHtmlNode( + '' + ), + 'to equal', + parseHtmlNode( + '' + ) + ); + }, + 'to throw an error satisfying to equal snapshot', + expect.unindent` + expected + to equal + + + ` + ); + }); + }); + + describe('on DOM document fragments', () => { + it('should succeeds if they are equal', () => { + expect( + parseHtmlFragment( + '

Tournament

' + ), + 'to equal', + parseHtmlFragment( + '

Tournament

' + ) + ); + }); + + it('should fail if they are not equal', () => { + expect( + () => { + expect( + parseHtmlFragment( + '

Tournament

' + ), + 'to equal', + parseHtmlFragment( + '

Tournament

' + ) + ); + }, + 'to throw an error satisfying to equal snapshot', + expect.unindent` + expected + DocumentFragment[NodeList[ +

Tournament

, + + ]] + to equal + DocumentFragment[NodeList[ +

Tournament

, + + ]] + +

Tournament

+ + ` + ); + }); + }); + + describe('on text nodes', () => { + it('should succeeds if they are equal', () => { + expect(parseHtml('text'), 'to equal', parseHtml('text')); + }); + + it('should fail if they are not equal', () => { + expect( + () => { + expect(parseHtml('text'), 'to equal', parseHtml('hext')); + }, + 'to throw an error satisfying to equal snapshot', + expect.unindent` + expected text to equal hext + + -text + +hext + ` + ); + }); + }); + + describe('on node lists', () => { + it('should succeeds if they are equal', () => { + expect( + parseHtmlFragment('
one
two
three
') + .childNodes, + 'to equal', + parseHtmlFragment('
one
two
three
') + .childNodes + ); + }); + + it('should fail if they are not equal', () => { + expect( + () => { + expect( + parseHtmlFragment('
one
two
three
') + .childNodes, + 'to equal', + parseHtmlFragment('
1
2
3
') + .childNodes + ); + }, + 'to throw an error satisfying to equal snapshot', + expect.unindent` + expected NodeList[
one
,
two
,
three
] + to equal NodeList[
1
,
2
,
3
] + + NodeList[ +
+ -one + +1 +
, +
+ -two + +2 +
, +
+ -three + +3 +
+ ] + ` + ); + }); + }); + }); + describe('to satisfy', () => { it('should fail if an unsupported property is passed in the value', () => { body.innerHTML = '
';