From 4a49aeb47188192eb049be4f759d3ec929a7fffa Mon Sep 17 00:00:00 2001 From: Rachel Franklin Date: Thu, 21 Mar 2019 13:28:19 +0000 Subject: [PATCH] docs: made toHaveBeenCalled example clearer (#8179) --- docs/ExpectAPI.md | 12 +++++++++--- website/versioned_docs/version-22.x/ExpectAPI.md | 12 +++++++++--- website/versioned_docs/version-23.x/ExpectAPI.md | 12 +++++++++--- website/versioned_docs/version-24.0/ExpectAPI.md | 12 +++++++++--- 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/docs/ExpectAPI.md b/docs/ExpectAPI.md index 114773883104..eec7e579d01d 100644 --- a/docs/ExpectAPI.md +++ b/docs/ExpectAPI.md @@ -575,17 +575,23 @@ Also under the alias: `.toBeCalled()` Use `.toHaveBeenCalled` to ensure that a mock function got called. -For example, let's say you have a `drinkAll(drink, flavor)` function that takes a `drink` function and applies it to all available beverages. You might want to check that `drink` gets called for `'lemon'`, but not for `'octopus'`, because `'octopus'` flavor is really weird and why would anything be octopus-flavored? You can do that with this test suite: +For example, let's say you have a `drinkAll(drink, flavour)` function that takes a `drink` function and applies it to all available beverages. You might want to check that `drink` gets called for `'lemon'`, but not for `'octopus'`, because `'octopus'` flavour is really weird and why would anything be octopus-flavoured? You can do that with this test suite: ```js +function drinkAll(callback, flavour) { + if (flavour !== 'octopus') { + callback(flavour); + } +} + describe('drinkAll', () => { - test('drinks something lemon-flavored', () => { + test('drinks something lemon-flavoured', () => { const drink = jest.fn(); drinkAll(drink, 'lemon'); expect(drink).toHaveBeenCalled(); }); - test('does not drink something octopus-flavored', () => { + test('does not drink something octopus-flavoured', () => { const drink = jest.fn(); drinkAll(drink, 'octopus'); expect(drink).not.toHaveBeenCalled(); diff --git a/website/versioned_docs/version-22.x/ExpectAPI.md b/website/versioned_docs/version-22.x/ExpectAPI.md index 57927be71fbb..d7eb70ebb37e 100644 --- a/website/versioned_docs/version-22.x/ExpectAPI.md +++ b/website/versioned_docs/version-22.x/ExpectAPI.md @@ -424,17 +424,23 @@ Also under the alias: `.toBeCalled()` Use `.toHaveBeenCalled` to ensure that a mock function got called. -For example, let's say you have a `drinkAll(drink, flavor)` function that takes a `drink` function and applies it to all available beverages. You might want to check that `drink` gets called for `'lemon'`, but not for `'octopus'`, because `'octopus'` flavor is really weird and why would anything be octopus-flavored? You can do that with this test suite: +For example, let's say you have a `drinkAll(drink, flavour)` function that takes a `drink` function and applies it to all available beverages. You might want to check that `drink` gets called for `'lemon'`, but not for `'octopus'`, because `'octopus'` flavour is really weird and why would anything be octopus-flavoured? You can do that with this test suite: ```js +function drinkAll(callback, flavour) { + if (flavour !== 'octopus') { + callback(flavour); + } +} + describe('drinkAll', () => { - test('drinks something lemon-flavored', () => { + test('drinks something lemon-flavoured', () => { const drink = jest.fn(); drinkAll(drink, 'lemon'); expect(drink).toHaveBeenCalled(); }); - test('does not drink something octopus-flavored', () => { + test('does not drink something octopus-flavoured', () => { const drink = jest.fn(); drinkAll(drink, 'octopus'); expect(drink).not.toHaveBeenCalled(); diff --git a/website/versioned_docs/version-23.x/ExpectAPI.md b/website/versioned_docs/version-23.x/ExpectAPI.md index 97719d342132..1bb4a8a6ea34 100644 --- a/website/versioned_docs/version-23.x/ExpectAPI.md +++ b/website/versioned_docs/version-23.x/ExpectAPI.md @@ -565,17 +565,23 @@ Also under the alias: `.toBeCalled()` Use `.toHaveBeenCalled` to ensure that a mock function got called. -For example, let's say you have a `drinkAll(drink, flavor)` function that takes a `drink` function and applies it to all available beverages. You might want to check that `drink` gets called for `'lemon'`, but not for `'octopus'`, because `'octopus'` flavor is really weird and why would anything be octopus-flavored? You can do that with this test suite: +For example, let's say you have a `drinkAll(drink, flavour)` function that takes a `drink` function and applies it to all available beverages. You might want to check that `drink` gets called for `'lemon'`, but not for `'octopus'`, because `'octopus'` flavour is really weird and why would anything be octopus-flavoured? You can do that with this test suite: ```js +function drinkAll(callback, flavour) { + if (flavour !== 'octopus') { + callback(flavour); + } +} + describe('drinkAll', () => { - test('drinks something lemon-flavored', () => { + test('drinks something lemon-flavoured', () => { const drink = jest.fn(); drinkAll(drink, 'lemon'); expect(drink).toHaveBeenCalled(); }); - test('does not drink something octopus-flavored', () => { + test('does not drink something octopus-flavoured', () => { const drink = jest.fn(); drinkAll(drink, 'octopus'); expect(drink).not.toHaveBeenCalled(); diff --git a/website/versioned_docs/version-24.0/ExpectAPI.md b/website/versioned_docs/version-24.0/ExpectAPI.md index dd6828349c4c..8a45ca4dced3 100644 --- a/website/versioned_docs/version-24.0/ExpectAPI.md +++ b/website/versioned_docs/version-24.0/ExpectAPI.md @@ -576,17 +576,23 @@ Also under the alias: `.toBeCalled()` Use `.toHaveBeenCalled` to ensure that a mock function got called. -For example, let's say you have a `drinkAll(drink, flavor)` function that takes a `drink` function and applies it to all available beverages. You might want to check that `drink` gets called for `'lemon'`, but not for `'octopus'`, because `'octopus'` flavor is really weird and why would anything be octopus-flavored? You can do that with this test suite: +For example, let's say you have a `drinkAll(drink, flavour)` function that takes a `drink` function and applies it to all available beverages. You might want to check that `drink` gets called for `'lemon'`, but not for `'octopus'`, because `'octopus'` flavour is really weird and why would anything be octopus-flavoured? You can do that with this test suite: ```js +function drinkAll(callback, flavour) { + if (flavour !== 'octopus') { + callback(flavour); + } +} + describe('drinkAll', () => { - test('drinks something lemon-flavored', () => { + test('drinks something lemon-flavoured', () => { const drink = jest.fn(); drinkAll(drink, 'lemon'); expect(drink).toHaveBeenCalled(); }); - test('does not drink something octopus-flavored', () => { + test('does not drink something octopus-flavoured', () => { const drink = jest.fn(); drinkAll(drink, 'octopus'); expect(drink).not.toHaveBeenCalled();