From fe144931752df61d27a26afb355f2e09b9ab6d61 Mon Sep 17 00:00:00 2001 From: Tien Do Date: Fri, 14 Jun 2019 17:47:07 +0700 Subject: [PATCH] docs: move toHaveLength and toHaveProperty to toHave* group (facebook#8564) (#8566) --- docs/ExpectAPI.md | 132 +++++++++--------- .../versioned_docs/version-22.x/ExpectAPI.md | 122 ++++++++-------- .../versioned_docs/version-23.x/ExpectAPI.md | 132 +++++++++--------- .../versioned_docs/version-24.0/ExpectAPI.md | 132 +++++++++--------- 4 files changed, 259 insertions(+), 259 deletions(-) diff --git a/docs/ExpectAPI.md b/docs/ExpectAPI.md index 25559f48bed2..2650e46df739 100644 --- a/docs/ExpectAPI.md +++ b/docs/ExpectAPI.md @@ -763,6 +763,72 @@ test('drink returns expected nth calls', () => { Note: the nth argument must be positive integer starting from 1. +### `.toHaveLength(number)` + +Use `.toHaveLength` to check that an object has a `.length` property and it is set to a certain numeric value. + +This is especially useful for checking arrays or strings size. + +```js +expect([1, 2, 3]).toHaveLength(3); +expect('abc').toHaveLength(3); +expect('').not.toHaveLength(5); +``` + +### `.toHaveProperty(keyPath, value?)` + +Use `.toHaveProperty` to check if property at provided reference `keyPath` exists for an object. For checking deeply nested properties in an object you may use [dot notation](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Property_accessors) or an array containing the keyPath for deep references. + +You can provide an optional `value` argument to compare the received property value (recursively for all properties of object instances, also known as deep equality, like the `toEqual` matcher). + +The following example contains a `houseForSale` object with nested properties. We are using `toHaveProperty` to check for the existence and values of various properties in the object. + +```js +// Object containing house features to be tested +const houseForSale = { + bath: true, + bedrooms: 4, + kitchen: { + amenities: ['oven', 'stove', 'washer'], + area: 20, + wallColor: 'white', + 'nice.oven': true, + }, + 'ceiling.height': 2, +}; + +test('this house has my desired features', () => { + // Simple Referencing + expect(houseForSale).toHaveProperty('bath'); + expect(houseForSale).toHaveProperty('bedrooms', 4); + + expect(houseForSale).not.toHaveProperty('pool'); + + // Deep referencing using dot notation + expect(houseForSale).toHaveProperty('kitchen.area', 20); + expect(houseForSale).toHaveProperty('kitchen.amenities', [ + 'oven', + 'stove', + 'washer', + ]); + + expect(houseForSale).not.toHaveProperty('kitchen.open'); + + // Deep referencing using an array containing the keyPath + expect(houseForSale).toHaveProperty(['kitchen', 'area'], 20); + expect(houseForSale).toHaveProperty( + ['kitchen', 'amenities'], + ['oven', 'stove', 'washer'], + ); + expect(houseForSale).toHaveProperty(['kitchen', 'amenities', 0], 'oven'); + expect(houseForSale).toHaveProperty(['kitchen', 'nice.oven']); + expect(houseForSale).not.toHaveProperty(['kitchen', 'open']); + + // Referencing keys with dot in the key itself + expect(houseForSale).toHaveProperty(['ceiling.height'], 'tall'); +}); +``` + ### `.toBeCloseTo(number, numDigits?)` Using exact equality with floating point numbers is a bad idea. Rounding means that intuitive things fail. For example, this test fails: @@ -988,18 +1054,6 @@ If differences between properties do not help you to understand why a test fails - rewrite `expect(received).toEqual(expected)` as `expect(received.equals(expected)).toBe(true)` - rewrite `expect(received).not.toEqual(expected)` as `expect(received.equals(expected)).toBe(false)` -### `.toHaveLength(number)` - -Use `.toHaveLength` to check that an object has a `.length` property and it is set to a certain numeric value. - -This is especially useful for checking arrays or strings size. - -```js -expect([1, 2, 3]).toHaveLength(3); -expect('abc').toHaveLength(3); -expect('').not.toHaveLength(5); -``` - ### `.toMatch(regexpOrString)` Use `.toMatch` to check that a string matches a regular expression. @@ -1071,60 +1125,6 @@ describe('toMatchObject applied to arrays', () => { }); ``` -### `.toHaveProperty(keyPath, value?)` - -Use `.toHaveProperty` to check if property at provided reference `keyPath` exists for an object. For checking deeply nested properties in an object you may use [dot notation](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Property_accessors) or an array containing the keyPath for deep references. - -You can provide an optional `value` argument to compare the received property value (recursively for all properties of object instances, also known as deep equality, like the `toEqual` matcher). - -The following example contains a `houseForSale` object with nested properties. We are using `toHaveProperty` to check for the existence and values of various properties in the object. - -```js -// Object containing house features to be tested -const houseForSale = { - bath: true, - bedrooms: 4, - kitchen: { - amenities: ['oven', 'stove', 'washer'], - area: 20, - wallColor: 'white', - 'nice.oven': true, - }, - 'ceiling.height': 2, -}; - -test('this house has my desired features', () => { - // Simple Referencing - expect(houseForSale).toHaveProperty('bath'); - expect(houseForSale).toHaveProperty('bedrooms', 4); - - expect(houseForSale).not.toHaveProperty('pool'); - - // Deep referencing using dot notation - expect(houseForSale).toHaveProperty('kitchen.area', 20); - expect(houseForSale).toHaveProperty('kitchen.amenities', [ - 'oven', - 'stove', - 'washer', - ]); - - expect(houseForSale).not.toHaveProperty('kitchen.open'); - - // Deep referencing using an array containing the keyPath - expect(houseForSale).toHaveProperty(['kitchen', 'area'], 20); - expect(houseForSale).toHaveProperty( - ['kitchen', 'amenities'], - ['oven', 'stove', 'washer'], - ); - expect(houseForSale).toHaveProperty(['kitchen', 'amenities', 0], 'oven'); - expect(houseForSale).toHaveProperty(['kitchen', 'nice.oven']); - expect(houseForSale).not.toHaveProperty(['kitchen', 'open']); - - // Referencing keys with dot in the key itself - expect(houseForSale).toHaveProperty(['ceiling.height'], 'tall'); -}); -``` - ### `.toMatchSnapshot(propertyMatchers?, hint?)` This ensures that a value matches the most recent snapshot. Check out [the Snapshot Testing guide](SnapshotTesting.md) for more information. diff --git a/website/versioned_docs/version-22.x/ExpectAPI.md b/website/versioned_docs/version-22.x/ExpectAPI.md index 96d194895144..aee02654789c 100644 --- a/website/versioned_docs/version-22.x/ExpectAPI.md +++ b/website/versioned_docs/version-22.x/ExpectAPI.md @@ -494,6 +494,67 @@ test('applying to all flavors does mango last', () => { }); ``` +### `.toHaveLength(number)` + +Use `.toHaveLength` to check that an object has a `.length` property and it is set to a certain numeric value. + +This is especially useful for checking arrays or strings size. + +```js +expect([1, 2, 3]).toHaveLength(3); +expect('abc').toHaveLength(3); +expect('').not.toHaveLength(5); +``` + +### `.toHaveProperty(keyPath, value)` + +Use `.toHaveProperty` to check if property at provided reference `keyPath` exists for an object. For checking deeply nested properties in an object you may use [dot notation](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Property_accessors) or an array containing the keyPath for deep references. + +Optionally, you can provide a `value` to check if it's equal to the value present at `keyPath` on the target object. This matcher uses 'deep equality' (like `toEqual()`) and recursively checks the equality of all fields. + +The following example contains a `houseForSale` object with nested properties. We are using `toHaveProperty` to check for the existence and values of various properties in the object. + +```js +// Object containing house features to be tested +const houseForSale = { + bath: true, + bedrooms: 4, + kitchen: { + amenities: ['oven', 'stove', 'washer'], + area: 20, + wallColor: 'white', + }, +}; + +test('this house has my desired features', () => { + // Simple Referencing + expect(houseForSale).toHaveProperty('bath'); + expect(houseForSale).toHaveProperty('bedrooms', 4); + + expect(houseForSale).not.toHaveProperty('pool'); + + // Deep referencing using dot notation + expect(houseForSale).toHaveProperty('kitchen.area', 20); + expect(houseForSale).toHaveProperty('kitchen.amenities', [ + 'oven', + 'stove', + 'washer', + ]); + + expect(houseForSale).not.toHaveProperty('kitchen.open'); + + // Deep referencing using an array containing the keyPath + expect(houseForSale).toHaveProperty(['kitchen', 'area'], 20); + expect(houseForSale).toHaveProperty( + ['kitchen', 'amenities'], + ['oven', 'stove', 'washer'], + ); + expect(houseForSale).toHaveProperty(['kitchen', 'amenities', 0], 'oven'); + + expect(houseForSale).not.toHaveProperty(['kitchen', 'open']); +}); +``` + ### `.toBeCloseTo(number, numDigits)` Using exact equality with floating point numbers is a bad idea. Rounding means that intuitive things fail. For example, this test fails: @@ -719,18 +780,6 @@ If differences between properties do not help you to understand why a test fails - rewrite `expect(received).toEqual(expected)` as `expect(received.equals(expected)).toBe(true)` - rewrite `expect(received).not.toEqual(expected)` as `expect(received.equals(expected)).toBe(false)` -### `.toHaveLength(number)` - -Use `.toHaveLength` to check that an object has a `.length` property and it is set to a certain numeric value. - -This is especially useful for checking arrays or strings size. - -```js -expect([1, 2, 3]).toHaveLength(3); -expect('abc').toHaveLength(3); -expect('').not.toHaveLength(5); -``` - ### `.toMatch(regexpOrString)` Use `.toMatch` to check that a string matches a regular expression. @@ -808,55 +857,6 @@ describe('toMatchObject applied to arrays', () => { }); ``` -### `.toHaveProperty(keyPath, value)` - -Use `.toHaveProperty` to check if property at provided reference `keyPath` exists for an object. For checking deeply nested properties in an object you may use [dot notation](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Property_accessors) or an array containing the keyPath for deep references. - -Optionally, you can provide a `value` to check if it's equal to the value present at `keyPath` on the target object. This matcher uses 'deep equality' (like `toEqual()`) and recursively checks the equality of all fields. - -The following example contains a `houseForSale` object with nested properties. We are using `toHaveProperty` to check for the existence and values of various properties in the object. - -```js -// Object containing house features to be tested -const houseForSale = { - bath: true, - bedrooms: 4, - kitchen: { - amenities: ['oven', 'stove', 'washer'], - area: 20, - wallColor: 'white', - }, -}; - -test('this house has my desired features', () => { - // Simple Referencing - expect(houseForSale).toHaveProperty('bath'); - expect(houseForSale).toHaveProperty('bedrooms', 4); - - expect(houseForSale).not.toHaveProperty('pool'); - - // Deep referencing using dot notation - expect(houseForSale).toHaveProperty('kitchen.area', 20); - expect(houseForSale).toHaveProperty('kitchen.amenities', [ - 'oven', - 'stove', - 'washer', - ]); - - expect(houseForSale).not.toHaveProperty('kitchen.open'); - - // Deep referencing using an array containing the keyPath - expect(houseForSale).toHaveProperty(['kitchen', 'area'], 20); - expect(houseForSale).toHaveProperty( - ['kitchen', 'amenities'], - ['oven', 'stove', 'washer'], - ); - expect(houseForSale).toHaveProperty(['kitchen', 'amenities', 0], 'oven'); - - expect(houseForSale).not.toHaveProperty(['kitchen', 'open']); -}); -``` - ### `.toMatchSnapshot(optionalString)` This ensures that a value matches the most recent snapshot. Check out [the Snapshot Testing guide](SnapshotTesting.md) for more information. diff --git a/website/versioned_docs/version-23.x/ExpectAPI.md b/website/versioned_docs/version-23.x/ExpectAPI.md index 9454863e7e75..6f312d10d250 100644 --- a/website/versioned_docs/version-23.x/ExpectAPI.md +++ b/website/versioned_docs/version-23.x/ExpectAPI.md @@ -753,6 +753,72 @@ test('drink returns expected nth calls', () => { Note: the nth argument must be positive integer starting from 1. +### `.toHaveLength(number)` + +Use `.toHaveLength` to check that an object has a `.length` property and it is set to a certain numeric value. + +This is especially useful for checking arrays or strings size. + +```js +expect([1, 2, 3]).toHaveLength(3); +expect('abc').toHaveLength(3); +expect('').not.toHaveLength(5); +``` + +### `.toHaveProperty(keyPath, value)` + +Use `.toHaveProperty` to check if property at provided reference `keyPath` exists for an object. For checking deeply nested properties in an object you may use [dot notation](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Property_accessors) or an array containing the keyPath for deep references. + +Optionally, you can provide a `value` to check if it's equal to the value present at `keyPath` on the target object. This matcher uses 'deep equality' (like `toEqual()`) and recursively checks the equality of all fields. + +The following example contains a `houseForSale` object with nested properties. We are using `toHaveProperty` to check for the existence and values of various properties in the object. + +```js +// Object containing house features to be tested +const houseForSale = { + bath: true, + bedrooms: 4, + kitchen: { + amenities: ['oven', 'stove', 'washer'], + area: 20, + wallColor: 'white', + 'nice.oven': true, + }, + 'ceiling.height': 2, +}; + +test('this house has my desired features', () => { + // Simple Referencing + expect(houseForSale).toHaveProperty('bath'); + expect(houseForSale).toHaveProperty('bedrooms', 4); + + expect(houseForSale).not.toHaveProperty('pool'); + + // Deep referencing using dot notation + expect(houseForSale).toHaveProperty('kitchen.area', 20); + expect(houseForSale).toHaveProperty('kitchen.amenities', [ + 'oven', + 'stove', + 'washer', + ]); + + expect(houseForSale).not.toHaveProperty('kitchen.open'); + + // Deep referencing using an array containing the keyPath + expect(houseForSale).toHaveProperty(['kitchen', 'area'], 20); + expect(houseForSale).toHaveProperty( + ['kitchen', 'amenities'], + ['oven', 'stove', 'washer'], + ); + expect(houseForSale).toHaveProperty(['kitchen', 'amenities', 0], 'oven'); + expect(houseForSale).toHaveProperty(['kitchen', 'nice.oven']); + expect(houseForSale).not.toHaveProperty(['kitchen', 'open']); + + // Referencing keys with dot in the key itself + expect(houseForSale).toHaveProperty(['ceiling.height'], 'tall'); +}); +``` + ### `.toBeCloseTo(number, numDigits)` Using exact equality with floating point numbers is a bad idea. Rounding means that intuitive things fail. For example, this test fails: @@ -978,18 +1044,6 @@ If differences between properties do not help you to understand why a test fails - rewrite `expect(received).toEqual(expected)` as `expect(received.equals(expected)).toBe(true)` - rewrite `expect(received).not.toEqual(expected)` as `expect(received.equals(expected)).toBe(false)` -### `.toHaveLength(number)` - -Use `.toHaveLength` to check that an object has a `.length` property and it is set to a certain numeric value. - -This is especially useful for checking arrays or strings size. - -```js -expect([1, 2, 3]).toHaveLength(3); -expect('abc').toHaveLength(3); -expect('').not.toHaveLength(5); -``` - ### `.toMatch(regexpOrString)` Use `.toMatch` to check that a string matches a regular expression. @@ -1067,60 +1121,6 @@ describe('toMatchObject applied to arrays', () => { }); ``` -### `.toHaveProperty(keyPath, value)` - -Use `.toHaveProperty` to check if property at provided reference `keyPath` exists for an object. For checking deeply nested properties in an object you may use [dot notation](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Property_accessors) or an array containing the keyPath for deep references. - -Optionally, you can provide a `value` to check if it's equal to the value present at `keyPath` on the target object. This matcher uses 'deep equality' (like `toEqual()`) and recursively checks the equality of all fields. - -The following example contains a `houseForSale` object with nested properties. We are using `toHaveProperty` to check for the existence and values of various properties in the object. - -```js -// Object containing house features to be tested -const houseForSale = { - bath: true, - bedrooms: 4, - kitchen: { - amenities: ['oven', 'stove', 'washer'], - area: 20, - wallColor: 'white', - 'nice.oven': true, - }, - 'ceiling.height': 2, -}; - -test('this house has my desired features', () => { - // Simple Referencing - expect(houseForSale).toHaveProperty('bath'); - expect(houseForSale).toHaveProperty('bedrooms', 4); - - expect(houseForSale).not.toHaveProperty('pool'); - - // Deep referencing using dot notation - expect(houseForSale).toHaveProperty('kitchen.area', 20); - expect(houseForSale).toHaveProperty('kitchen.amenities', [ - 'oven', - 'stove', - 'washer', - ]); - - expect(houseForSale).not.toHaveProperty('kitchen.open'); - - // Deep referencing using an array containing the keyPath - expect(houseForSale).toHaveProperty(['kitchen', 'area'], 20); - expect(houseForSale).toHaveProperty( - ['kitchen', 'amenities'], - ['oven', 'stove', 'washer'], - ); - expect(houseForSale).toHaveProperty(['kitchen', 'amenities', 0], 'oven'); - expect(houseForSale).toHaveProperty(['kitchen', 'nice.oven']); - expect(houseForSale).not.toHaveProperty(['kitchen', 'open']); - - // Referencing keys with dot in the key itself - expect(houseForSale).toHaveProperty(['ceiling.height'], 'tall'); -}); -``` - ### `.toMatchSnapshot(propertyMatchers, snapshotName)` This ensures that a value matches the most recent snapshot. Check out [the Snapshot Testing guide](SnapshotTesting.md) for more information. diff --git a/website/versioned_docs/version-24.0/ExpectAPI.md b/website/versioned_docs/version-24.0/ExpectAPI.md index 6213cc6c8ee1..291d30705117 100644 --- a/website/versioned_docs/version-24.0/ExpectAPI.md +++ b/website/versioned_docs/version-24.0/ExpectAPI.md @@ -764,6 +764,72 @@ test('drink returns expected nth calls', () => { Note: the nth argument must be positive integer starting from 1. +### `.toHaveLength(number)` + +Use `.toHaveLength` to check that an object has a `.length` property and it is set to a certain numeric value. + +This is especially useful for checking arrays or strings size. + +```js +expect([1, 2, 3]).toHaveLength(3); +expect('abc').toHaveLength(3); +expect('').not.toHaveLength(5); +``` + +### `.toHaveProperty(keyPath, value?)` + +Use `.toHaveProperty` to check if property at provided reference `keyPath` exists for an object. For checking deeply nested properties in an object you may use [dot notation](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Property_accessors) or an array containing the keyPath for deep references. + +You can provide an optional `value` argument to compare the received property value (recursively for all properties of object instances, also known as deep equality, like the `toEqual` matcher). + +The following example contains a `houseForSale` object with nested properties. We are using `toHaveProperty` to check for the existence and values of various properties in the object. + +```js +// Object containing house features to be tested +const houseForSale = { + bath: true, + bedrooms: 4, + kitchen: { + amenities: ['oven', 'stove', 'washer'], + area: 20, + wallColor: 'white', + 'nice.oven': true, + }, + 'ceiling.height': 2, +}; + +test('this house has my desired features', () => { + // Simple Referencing + expect(houseForSale).toHaveProperty('bath'); + expect(houseForSale).toHaveProperty('bedrooms', 4); + + expect(houseForSale).not.toHaveProperty('pool'); + + // Deep referencing using dot notation + expect(houseForSale).toHaveProperty('kitchen.area', 20); + expect(houseForSale).toHaveProperty('kitchen.amenities', [ + 'oven', + 'stove', + 'washer', + ]); + + expect(houseForSale).not.toHaveProperty('kitchen.open'); + + // Deep referencing using an array containing the keyPath + expect(houseForSale).toHaveProperty(['kitchen', 'area'], 20); + expect(houseForSale).toHaveProperty( + ['kitchen', 'amenities'], + ['oven', 'stove', 'washer'], + ); + expect(houseForSale).toHaveProperty(['kitchen', 'amenities', 0], 'oven'); + expect(houseForSale).toHaveProperty(['kitchen', 'nice.oven']); + expect(houseForSale).not.toHaveProperty(['kitchen', 'open']); + + // Referencing keys with dot in the key itself + expect(houseForSale).toHaveProperty(['ceiling.height'], 'tall'); +}); +``` + ### `.toBeCloseTo(number, numDigits?)` Using exact equality with floating point numbers is a bad idea. Rounding means that intuitive things fail. For example, this test fails: @@ -989,18 +1055,6 @@ If differences between properties do not help you to understand why a test fails - rewrite `expect(received).toEqual(expected)` as `expect(received.equals(expected)).toBe(true)` - rewrite `expect(received).not.toEqual(expected)` as `expect(received.equals(expected)).toBe(false)` -### `.toHaveLength(number)` - -Use `.toHaveLength` to check that an object has a `.length` property and it is set to a certain numeric value. - -This is especially useful for checking arrays or strings size. - -```js -expect([1, 2, 3]).toHaveLength(3); -expect('abc').toHaveLength(3); -expect('').not.toHaveLength(5); -``` - ### `.toMatch(regexpOrString)` Use `.toMatch` to check that a string matches a regular expression. @@ -1078,60 +1132,6 @@ describe('toMatchObject applied to arrays', () => { }); ``` -### `.toHaveProperty(keyPath, value?)` - -Use `.toHaveProperty` to check if property at provided reference `keyPath` exists for an object. For checking deeply nested properties in an object you may use [dot notation](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Property_accessors) or an array containing the keyPath for deep references. - -You can provide an optional `value` argument to compare the received property value (recursively for all properties of object instances, also known as deep equality, like the `toEqual` matcher). - -The following example contains a `houseForSale` object with nested properties. We are using `toHaveProperty` to check for the existence and values of various properties in the object. - -```js -// Object containing house features to be tested -const houseForSale = { - bath: true, - bedrooms: 4, - kitchen: { - amenities: ['oven', 'stove', 'washer'], - area: 20, - wallColor: 'white', - 'nice.oven': true, - }, - 'ceiling.height': 2, -}; - -test('this house has my desired features', () => { - // Simple Referencing - expect(houseForSale).toHaveProperty('bath'); - expect(houseForSale).toHaveProperty('bedrooms', 4); - - expect(houseForSale).not.toHaveProperty('pool'); - - // Deep referencing using dot notation - expect(houseForSale).toHaveProperty('kitchen.area', 20); - expect(houseForSale).toHaveProperty('kitchen.amenities', [ - 'oven', - 'stove', - 'washer', - ]); - - expect(houseForSale).not.toHaveProperty('kitchen.open'); - - // Deep referencing using an array containing the keyPath - expect(houseForSale).toHaveProperty(['kitchen', 'area'], 20); - expect(houseForSale).toHaveProperty( - ['kitchen', 'amenities'], - ['oven', 'stove', 'washer'], - ); - expect(houseForSale).toHaveProperty(['kitchen', 'amenities', 0], 'oven'); - expect(houseForSale).toHaveProperty(['kitchen', 'nice.oven']); - expect(houseForSale).not.toHaveProperty(['kitchen', 'open']); - - // Referencing keys with dot in the key itself - expect(houseForSale).toHaveProperty(['ceiling.height'], 'tall'); -}); -``` - ### `.toMatchSnapshot(propertyMatchers?, hint?)` This ensures that a value matches the most recent snapshot. Check out [the Snapshot Testing guide](SnapshotTesting.md) for more information.