Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Update .toHaveProperty tests to reflect change and test against array…
…s as keyPath arguments
  • Loading branch information
Dillon Mulroy committed Jan 3, 2018
commit e930627386bc52249a444a1bba3e2affc5d22135
110 changes: 107 additions & 3 deletions packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2562,21 +2562,21 @@ Expected value to have a 'length' property that is a number. Received:
exports[`.toHaveProperty() {error} expect({"a": {"b": {}}}).toHaveProperty('1') 1`] = `
"<dim>expect(</><red>object</><dim>)[.not].toHaveProperty(</><green>path</><dim>)</>

Expected <green>path</> to be a string. Received:
Expected <green>path</> to be a string or an array. Received:
number: <red>1</>"
`;

exports[`.toHaveProperty() {error} expect({"a": {"b": {}}}).toHaveProperty('null') 1`] = `
"<dim>expect(</><red>object</><dim>)[.not].toHaveProperty(</><green>path</><dim>)</>

Expected <green>path</> to be a string. Received:
Expected <green>path</> to be a string or an array. Received:
null: <red>null</>"
`;

exports[`.toHaveProperty() {error} expect({"a": {"b": {}}}).toHaveProperty('undefined') 1`] = `
"<dim>expect(</><red>object</><dim>)[.not].toHaveProperty(</><green>path</><dim>)</>

Expected <green>path</> to be a string. Received:
Expected <green>path</> to be a string or an array. Received:
undefined: <red>undefined</>"
`;

Expand Down Expand Up @@ -2616,6 +2616,19 @@ With a value of:
"
`;

exports[`.toHaveProperty() {pass: false} expect({"a": {"b": {"c": {"d": 1}}}}).toHaveProperty('a,b,c,d', 2) 1`] = `
"<dim>expect(</><red>object</><dim>).toHaveProperty(</><green>path</>, <green>value</><dim>)</>

Expected the object:
<red>{\\"a\\": {\\"b\\": {\\"c\\": {\\"d\\": 1}}}}</>
To have a nested property:
<green>[\\"a\\", \\"b\\", \\"c\\", \\"d\\"]</>
With a value of:
<green>2</>
Received:
<red>1</>"
`;

exports[`.toHaveProperty() {pass: false} expect({"a": {"b": {"c": {"d": 1}}}}).toHaveProperty('a.b.c.d', 2) 1`] = `
"<dim>expect(</><red>object</><dim>).toHaveProperty(</><green>path</>, <green>value</><dim>)</>

Expand Down Expand Up @@ -2730,6 +2743,31 @@ Received:
<red>object</>.a: <red>1</>"
`;

exports[`.toHaveProperty() {pass: false} expect({"a.b.c.d": 1}).toHaveProperty('a.b.c.d', 2) 1`] = `
"<dim>expect(</><red>object</><dim>).toHaveProperty(</><green>path</>, <green>value</><dim>)</>

Expected the object:
<red>{\\"a.b.c.d\\": 1}</>
To have a nested property:
<green>\\"a.b.c.d\\"</>
With a value of:
<green>2</>
"
`;

exports[`.toHaveProperty() {pass: false} expect({"a.b.c.d": 1}).toHaveProperty('a.b.c.d', 2) 2`] = `
"<dim>expect(</><red>object</><dim>).toHaveProperty(</><green>path</>, <green>value</><dim>)</>

Expected the object:
<red>{\\"a.b.c.d\\": 1}</>
To have a nested property:
<green>[\\"a.b.c.d\\"]</>
With a value of:
<green>2</>
Received:
<red>1</>"
`;

exports[`.toHaveProperty() {pass: false} expect({}).toHaveProperty('a') 1`] = `
"<dim>expect(</><red>object</><dim>).toHaveProperty(</><green>path</><dim>)</>

Expand Down Expand Up @@ -2774,6 +2812,50 @@ With a value of:
"
`;

exports[`.toHaveProperty() {pass: true} expect({"a": {"b": [1, 2, 3]}}).toHaveProperty('a,b,1')' 1`] = `
"<dim>expect(</><red>object</><dim>).not.toHaveProperty(</><green>path</><dim>)</>

Expected the object:
<red>{\\"a\\": {\\"b\\": [1, 2, 3]}}</>
Not to have a nested property:
<green>[\\"a\\", \\"b\\", 1]</>
"
`;

exports[`.toHaveProperty() {pass: true} expect({"a": {"b": [1, 2, 3]}}).toHaveProperty('a,b,1', 2) 1`] = `
"<dim>expect(</><red>object</><dim>).not.toHaveProperty(</><green>path</>, <green>value</><dim>)</>

Expected the object:
<red>{\\"a\\": {\\"b\\": [1, 2, 3]}}</>
Not to have a nested property:
<green>[\\"a\\", \\"b\\", 1]</>
With a value of:
<green>2</>
"
`;

exports[`.toHaveProperty() {pass: true} expect({"a": {"b": {"c": {"d": 1}}}}).toHaveProperty('a,b,c,d')' 1`] = `
Copy link
Contributor

Choose a reason for hiding this comment

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

Btw, this is out of scope of this PR, but could you please remove this extra single-quote here?
Also, not super relevant, but this should say .toHaveProperty(['a','b','c','d']) instead .toHaveProperty('a,b,c,d')

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I fixed the changelog and remove the extra single-quote. How would you prefer for the snapshot output be changed (if at all for this PR)? I tried using the stringify util function but ended up with a different issue where the output looked like .toHaveProperty('["a","b","c","d"]')

Copy link
Contributor

Choose a reason for hiding this comment

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

If I had to choose, I'd pick the stringified version (it's just a name of the test anyway)

Copy link
Contributor Author

@dmmulroy dmmulroy Jan 3, 2018

Choose a reason for hiding this comment

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

Taking a second look, it also had an adverse effect of adding additional double quotes to normal keypaths like so : exports['.toHaveProperty() {pass: false} expect("abc").toHaveProperty('"a.b.c"') 1']. This might be better off fixed in the pretty-format package in a separate PR to prevent really cluttering up the matcher tests with string parsing logic.

"<dim>expect(</><red>object</><dim>).not.toHaveProperty(</><green>path</><dim>)</>

Expected the object:
<red>{\\"a\\": {\\"b\\": {\\"c\\": {\\"d\\": 1}}}}</>
Not to have a nested property:
<green>[\\"a\\", \\"b\\", \\"c\\", \\"d\\"]</>
"
`;

exports[`.toHaveProperty() {pass: true} expect({"a": {"b": {"c": {"d": 1}}}}).toHaveProperty('a,b,c,d', 1) 1`] = `
"<dim>expect(</><red>object</><dim>).not.toHaveProperty(</><green>path</>, <green>value</><dim>)</>

Expected the object:
<red>{\\"a\\": {\\"b\\": {\\"c\\": {\\"d\\": 1}}}}</>
Not to have a nested property:
<green>[\\"a\\", \\"b\\", \\"c\\", \\"d\\"]</>
With a value of:
<green>1</>
"
`;

exports[`.toHaveProperty() {pass: true} expect({"a": {"b": {"c": {"d": 1}}}}).toHaveProperty('a.b.c.d')' 1`] = `
"<dim>expect(</><red>object</><dim>).not.toHaveProperty(</><green>path</><dim>)</>

Expand Down Expand Up @@ -2852,6 +2934,28 @@ With a value of:
"
`;

exports[`.toHaveProperty() {pass: true} expect({"a.b.c.d": 1}).toHaveProperty('a.b.c.d')' 1`] = `
"<dim>expect(</><red>object</><dim>).not.toHaveProperty(</><green>path</><dim>)</>

Expected the object:
<red>{\\"a.b.c.d\\": 1}</>
Not to have a nested property:
<green>[\\"a.b.c.d\\"]</>
"
`;

exports[`.toHaveProperty() {pass: true} expect({"a.b.c.d": 1}).toHaveProperty('a.b.c.d', 1) 1`] = `
"<dim>expect(</><red>object</><dim>).not.toHaveProperty(</><green>path</>, <green>value</><dim>)</>

Expected the object:
<red>{\\"a.b.c.d\\": 1}</>
Not to have a nested property:
<green>[\\"a.b.c.d\\"]</>
With a value of:
<green>1</>
"
`;

exports[`.toHaveProperty() {pass: true} expect({"property": 1}).toHaveProperty('property', 1) 1`] = `
"<dim>expect(</><red>object</><dim>).not.toHaveProperty(</><green>path</>, <green>value</><dim>)</>

Expand Down
9 changes: 9 additions & 0 deletions packages/expect/src/__tests__/matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,9 @@ describe('.toHaveLength', () => {
describe('.toHaveProperty()', () => {
[
[{a: {b: {c: {d: 1}}}}, 'a.b.c.d', 1],
[{a: {b: {c: {d: 1}}}}, ['a', 'b', 'c', 'd'], 1],
[{'a.b.c.d': 1}, ['a.b.c.d'], 1],
[{a: {b: [1, 2, 3]}}, ['a', 'b', 1], 2],
[{a: 0}, 'a', 0],
[{a: {b: undefined}}, 'a.b', undefined],
[{a: {b: {c: 5}}}, 'a.b', {c: 5}],
Expand All @@ -769,6 +772,9 @@ describe('.toHaveProperty()', () => {
[
[{a: {b: {c: {d: 1}}}}, 'a.b.ttt.d', 1],
[{a: {b: {c: {d: 1}}}}, 'a.b.c.d', 2],
[{'a.b.c.d': 1}, 'a.b.c.d', 2],
[{'a.b.c.d': 1}, ['a.b.c.d'], 2],
[{a: {b: {c: {d: 1}}}}, ['a', 'b', 'c', 'd'], 2],
[{a: {b: {c: {}}}}, 'a.b.c.d', 1],
[{a: 1}, 'a.b.c.d', 5],
[{}, 'a', 'test'],
Expand All @@ -789,6 +795,9 @@ describe('.toHaveProperty()', () => {

[
[{a: {b: {c: {d: 1}}}}, 'a.b.c.d'],
[{a: {b: {c: {d: 1}}}}, ['a', 'b', 'c', 'd']],
[{'a.b.c.d': 1}, ['a.b.c.d']],
[{a: {b: [1, 2, 3]}}, ['a', 'b', 1]],
[{a: 0}, 'a'],
[{a: {b: undefined}}, 'a.b'],
].forEach(([obj, keyPath]) => {
Expand Down