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

<DOMDocument|DOMElement|DOMDocumentFragment> [not] to contain <DOMElement|object> #255

Merged
merged 20 commits into from
Feb 27, 2019

Conversation

sunesimonsen
Copy link
Member

@sunesimonsen sunesimonsen commented Feb 22, 2019

Fixes: #254

@sunesimonsen sunesimonsen force-pushed the ssimonsen/to-contain-spec branch 3 times, most recently from 3ffdb5f to 76beb4e Compare February 24, 2019 18:53
@sunesimonsen sunesimonsen changed the title [WIP] <DOMDocument|DOMElement|DOMDocumentFragment> to contain <DOMElement|object> <DOMDocument|DOMElement|DOMDocumentFragment> to contain <DOMElement|object> Feb 25, 2019
@coveralls
Copy link

coveralls commented Feb 25, 2019

Pull Request Test Coverage Report for Build 1063

  • 130 of 130 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+2.2%) to 93.75%

Totals Coverage Status
Change from base Build 1014: 2.2%
Covered Lines: 649
Relevant Lines: 677

💛 - Coveralls

@papandreou
Copy link
Member

Looks promising!

Would be nice with a not to contain variant, but I guess that would require a different kind of diff to be implemented?

Is it intentional that the assertion passes when the subject itself matches the spec? There's no test that covers it, so I can't really tell. This passes:

expect(
  parseHtmlNode('<span id="foo"></span>'),
  'to contain',
  '<span id="foo"></span>'
);

I'd argue that it doesn't really contain a match in that case. On the other hand, I don't really know what would be more useful.

The support for a RHS string seems broken:

expect(parseHtmlNode('<span>hello</span>'), 'to contain', 'hello');
Error: Unsupported options: 0, 1, 2, 3, 4

@sunesimonsen
Copy link
Member Author

@papandreou thanks for the feedback.

Is it intentional that the assertion passes when the subject itself matches the spec? There's no test that covers it, so I can't really tell. This passes:

It was, but looking at to contain elements matching I can see that it only looks at the descendant elements, so this assertion should behave the same. I'll fix that.

The support for a RHS string seems broken

This is a bit more tricky, as you would normally do something like this:

expect(
  parseHtmlNode('<div><span class="greeting">Hello</span><span class="name">Jane Doe</span></div>'), 
  'to contain', 
  '<span class="name">Jane Doe</span>'
);

But I think it would be nice if this also worked:

expect(
  parseHtmlNode('<div><span class="greeting">Hello</span><span class="name">Jane Doe</span></div>'), 
  'to contain', 
  'Jane Doe'
);

I'll see what I can do.

@alexjeffburke
Copy link
Member

I've done some thinking about the semantics and I tend to agree with Andreas - one of the things we know from the past is overly intelligent matching has almost always come to bite us. In this case, it feels right that "to contain" matching should perhaps require children or an array before it will go searching for a match - and it makes it much easier to explain when to use this versus using "to satisfy" on an element.

About the string matching, hmm - I'm not sure. It would be nice it works but again I'm a bit worried that it becomes a bit magic - the behaviouraround what will be found needs to be super clear. By that reasoning the semantics of "... to contain <string>" would be find a text node where that entire string was its content and nothing else. Does that make sense?

@sunesimonsen
Copy link
Member Author

@alexjeffburke and @papandreou I fixed both the cases you pointed out.

@sunesimonsen
Copy link
Member Author

sunesimonsen commented Feb 26, 2019

@papandreou about not to contain, we can just use the current code to see if we can find a good match, if that matches the assertion should fail. For the diff we could try to clone the subject and remove all matching elements (or maybe only the best one) and use to satisfy against the modified clone. I think that will give you, should be removed.

@sunesimonsen sunesimonsen changed the title <DOMDocument|DOMElement|DOMDocumentFragment> to contain <DOMElement|object> <DOMDocument|DOMElement|DOMDocumentFragment> [not] to contain <DOMElement|object> Feb 26, 2019
Notice that the diff can be improved
src/index.js Outdated Show resolved Hide resolved
@alexjeffburke
Copy link
Member

alexjeffburke commented Feb 26, 2019

@sunesimonsen I think the removal of the text support is a very good thing - it'll keep the semantics clean and just sidestep any pontetial surprise factor.

Copy link
Member

@papandreou papandreou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, just some small nitpicks.


if (typeof spec === 'string') {
throw new Error(
'HTMLElement to contain string: please provide a HTML structure as a string'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for full disclosure -- IMO it'd be fine to support a RHS of a string without HTML tags wrapped around it -- as long as < etc. are interpreted as HTML rather than text.

We can also add that later, so nbd.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@papandreou I'm leaning towards having an explicit
to contain text assertion, that also mirror the current to have text and to satisfy assertions better. What du you think about that idea?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That could be a fine addition of its own. I still think <DOMDocument|DOMElement|...> to contain <string> should behave as I suggest above.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I think that is what we had, but I saw some weirdness around HTML entities so I just reverted it. Let's see if we can reintroduce it in a later PR.

src/index.js Outdated Show resolved Hide resolved
src/index.js Outdated Show resolved Hide resolved
src/index.js Outdated Show resolved Hide resolved
src/index.js Outdated Show resolved Hide resolved
src/index.js Show resolved Hide resolved
src/index.js Show resolved Hide resolved
@sunesimonsen sunesimonsen merged commit 484927e into master Feb 27, 2019
@sunesimonsen sunesimonsen deleted the ssimonsen/to-contain-spec branch February 27, 2019 08:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants