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

docs: runnable example of the for Each test #13428

Merged
merged 35 commits into from
Jan 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
65cfe82
Runnable example of the for Each test
Oct 11, 2022
6f43205
Merge branch 'facebook:main' into patch-1
Dec 14, 2022
3a88b69
Update website/versioned_docs/version-29.1/MockFunctions.md
Dec 15, 2022
38afc48
Update website/versioned_docs/version-29.1/MockFunctions.md
Dec 15, 2022
8ef4867
Changed docs around all the versions for mockFunc
Dec 15, 2022
9398746
single quotes
Dec 15, 2022
4753f97
basic working example
Dec 15, 2022
01bf339
.toHaveLength()
Dec 15, 2022
5ae1f95
Update website/versioned_docs/version-25.x/MockFunctions.md
Dec 16, 2022
ebc6112
also update docs main dir + prettier lint
Dec 16, 2022
544a9c7
Calls.length -> toHaveLength()
Dec 16, 2022
05cca48
ESM Syntax
Dec 16, 2022
5bd950f
change title to forEach
Dec 16, 2022
c98ed64
undo unmeant doc changes to v25
Dec 16, 2022
1222c5d
remove mymock1
Dec 16, 2022
988db29
space
Dec 16, 2022
2414a55
Empty lines undov25
Dec 19, 2022
2f7efae
/docs adjustment
Dec 19, 2022
f467915
V28 mockFunctions
Dec 19, 2022
9aaa5dd
mockfunction v26
Dec 19, 2022
6cf4e4a
mockFunction v29
Dec 19, 2022
f1cae77
mockfunction v29.1
Dec 19, 2022
b1eac2c
mockFunctions 29.2
Dec 19, 2022
817d3cd
mockFunctions v29.3
Dec 19, 2022
11aed76
docs type foEach
Dec 19, 2022
9b893b2
context mock functions
Dec 19, 2022
408dc62
seperate line filter test
Dec 19, 2022
f75b8a4
mockcontext 29.0
Dec 19, 2022
e01bb92
mockcontext v29.1
Dec 19, 2022
25dbb66
mock.bind revert from v28 onwards
Dec 21, 2022
3658f8c
.mock prop
Dec 21, 2022
77be83a
remove space
Dec 21, 2022
b0d31c1
enter and close function block
Dec 21, 2022
737fb4d
close function block
Dec 21, 2022
0d91ee2
v28 cloase block
Dec 21, 2022
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
31 changes: 18 additions & 13 deletions docs/MockFunctions.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ There are two ways to mock functions: Either by creating a mock function to use

Let's imagine we're testing an implementation of a function `forEach`, which invokes a callback for each item in a supplied array.

```javascript
function forEach(items, callback) {
```js title="forEach.js"
export function forEach(items, callback) {
for (let index = 0; index < items.length; index++) {
callback(items[index]);
}
Expand All @@ -21,21 +21,26 @@ function forEach(items, callback) {

To test this function, we can use a mock function, and inspect the mock's state to ensure the callback is invoked as expected.

```javascript
```js title="forEach.test.js"
const forEach = require('./forEach');

const mockCallback = jest.fn(x => 42 + x);
forEach([0, 1], mockCallback);

// The mock function is called twice
expect(mockCallback.mock.calls.length).toBe(2);
test('forEach mock function', () => {
forEach([0, 1], mockCallback);

// The first argument of the first call to the function was 0
expect(mockCallback.mock.calls[0][0]).toBe(0);
// The mock function was called twice
expect(mockCallback.mock.calls).toHaveLength(2);

// The first argument of the second call to the function was 1
expect(mockCallback.mock.calls[1][0]).toBe(1);
// The first argument of the first call to the function was 0
expect(mockCallback.mock.calls[0][0]).toBe(0);

// The return value of the first call to the function was 42
expect(mockCallback.mock.results[0].value).toBe(42);
// The first argument of the second call to the function was 1
expect(mockCallback.mock.calls[1][0]).toBe(1);

// The return value of the first call to the function was 42
expect(mockCallback.mock.results[0].value).toBe(42);
});
```

## `.mock` property
Expand All @@ -60,7 +65,7 @@ These mock members are very useful in tests to assert how these functions get ca

```javascript
// The function was called exactly once
expect(someMockFunction.mock.calls.length).toBe(1);
expect(someMockFunction.mock.calls).toHaveLength(1);

// The first arg of the first call to the function was 'first arg'
expect(someMockFunction.mock.calls[0][0]).toBe('first arg');
Expand Down
31 changes: 18 additions & 13 deletions website/versioned_docs/version-25.x/MockFunctions.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ There are two ways to mock functions: Either by creating a mock function to use

Let's imagine we're testing an implementation of a function `forEach`, which invokes a callback for each item in a supplied array.

```javascript
function forEach(items, callback) {
```js title="forEach.js"
export function forEach(items, callback) {
for (let index = 0; index < items.length; index++) {
callback(items[index]);
}
Expand All @@ -21,21 +21,26 @@ function forEach(items, callback) {

To test this function, we can use a mock function, and inspect the mock's state to ensure the callback is invoked as expected.

```javascript
```js title="forEach.test.js"
import {forEach} from './forEach';

const mockCallback = jest.fn(x => 42 + x);
forEach([0, 1], mockCallback);

// The mock function is called twice
expect(mockCallback.mock.calls.length).toBe(2);
test('forEach function', () => {
forEach([0, 1], mockCallback);

// The first argument of the first call to the function was 0
expect(mockCallback.mock.calls[0][0]).toBe(0);
// The mock function was called twice
expect(mockCallback.mock.calls).toHaveLength(2);
Jeroendevr marked this conversation as resolved.
Show resolved Hide resolved

// The first argument of the second call to the function was 1
expect(mockCallback.mock.calls[1][0]).toBe(1);
// The first argument of the first call to the function was 0
expect(mockCallback.mock.calls[0][0]).toBe(0);

// The return value of the first call to the function was 42
expect(mockCallback.mock.results[0].value).toBe(42);
// The first argument of the second call to the function was 1
expect(mockCallback.mock.calls[1][0]).toBe(1);

// The return value of the first call to the function was 42
expect(mockCallback.mock.results[0].value).toBe(42);
});
```

## `.mock` property
Expand All @@ -58,7 +63,7 @@ These mock members are very useful in tests to assert how these functions get ca

```javascript
// The function was called exactly once
expect(someMockFunction.mock.calls.length).toBe(1);
expect(someMockFunction.mock.calls).toHaveLength(1);

// The first arg of the first call to the function was 'first arg'
expect(someMockFunction.mock.calls[0][0]).toBe('first arg');
Expand Down
31 changes: 18 additions & 13 deletions website/versioned_docs/version-26.x/MockFunctions.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ There are two ways to mock functions: Either by creating a mock function to use

Let's imagine we're testing an implementation of a function `forEach`, which invokes a callback for each item in a supplied array.

```javascript
function forEach(items, callback) {
```js title="forEach.js"
export function forEach(items, callback) {
for (let index = 0; index < items.length; index++) {
callback(items[index]);
}
Expand All @@ -21,21 +21,26 @@ function forEach(items, callback) {

To test this function, we can use a mock function, and inspect the mock's state to ensure the callback is invoked as expected.

```javascript
```js title="forEach.test.js"
const forEach = require('./forEach');

const mockCallback = jest.fn(x => 42 + x);
forEach([0, 1], mockCallback);

// The mock function is called twice
expect(mockCallback.mock.calls.length).toBe(2);
test('forEach mock function', () => {
forEach([0, 1], mockCallback);

// The first argument of the first call to the function was 0
expect(mockCallback.mock.calls[0][0]).toBe(0);
// The mock function was called twice
expect(mockCallback.mock.calls).toHaveLength(2);

// The first argument of the second call to the function was 1
expect(mockCallback.mock.calls[1][0]).toBe(1);
// The first argument of the first call to the function was 0
expect(mockCallback.mock.calls[0][0]).toBe(0);

// The return value of the first call to the function was 42
expect(mockCallback.mock.results[0].value).toBe(42);
// The first argument of the second call to the function was 1
expect(mockCallback.mock.calls[1][0]).toBe(1);

// The return value of the first call to the function was 42
expect(mockCallback.mock.results[0].value).toBe(42);
});
```

## `.mock` property
Expand All @@ -58,7 +63,7 @@ These mock members are very useful in tests to assert how these functions get ca

```javascript
// The function was called exactly once
expect(someMockFunction.mock.calls.length).toBe(1);
expect(someMockFunction.mock.calls).toHaveLength(1);

// The first arg of the first call to the function was 'first arg'
expect(someMockFunction.mock.calls[0][0]).toBe('first arg');
Expand Down
31 changes: 18 additions & 13 deletions website/versioned_docs/version-27.x/MockFunctions.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ There are two ways to mock functions: Either by creating a mock function to use

Let's imagine we're testing an implementation of a function `forEach`, which invokes a callback for each item in a supplied array.

```javascript
function forEach(items, callback) {
```js title="forEach.js"
export function forEach(items, callback) {
for (let index = 0; index < items.length; index++) {
callback(items[index]);
}
Expand All @@ -21,21 +21,26 @@ function forEach(items, callback) {

To test this function, we can use a mock function, and inspect the mock's state to ensure the callback is invoked as expected.

```javascript
```js title="forEach.test.js"
const forEach = require('./forEach');

const mockCallback = jest.fn(x => 42 + x);
forEach([0, 1], mockCallback);

// The mock function is called twice
expect(mockCallback.mock.calls.length).toBe(2);
test('forEach mock function', () => {
forEach([0, 1], mockCallback);

// The first argument of the first call to the function was 0
expect(mockCallback.mock.calls[0][0]).toBe(0);
// The mock function was called twice
expect(mockCallback.mock.calls).toHaveLength(2);

// The first argument of the second call to the function was 1
expect(mockCallback.mock.calls[1][0]).toBe(1);
// The first argument of the first call to the function was 0
expect(mockCallback.mock.calls[0][0]).toBe(0);

// The return value of the first call to the function was 42
expect(mockCallback.mock.results[0].value).toBe(42);
// The first argument of the second call to the function was 1
expect(mockCallback.mock.calls[1][0]).toBe(1);

// The return value of the first call to the function was 42
expect(mockCallback.mock.results[0].value).toBe(42);
});
```

## `.mock` property
Expand All @@ -58,7 +63,7 @@ These mock members are very useful in tests to assert how these functions get ca

```javascript
// The function was called exactly once
expect(someMockFunction.mock.calls.length).toBe(1);
expect(someMockFunction.mock.calls).toHaveLength(1);

// The first arg of the first call to the function was 'first arg'
expect(someMockFunction.mock.calls[0][0]).toBe('first arg');
Expand Down
31 changes: 18 additions & 13 deletions website/versioned_docs/version-28.x/MockFunctions.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ There are two ways to mock functions: Either by creating a mock function to use

Let's imagine we're testing an implementation of a function `forEach`, which invokes a callback for each item in a supplied array.

```javascript
function forEach(items, callback) {
```js title="forEach.js"
export function forEach(items, callback) {
for (let index = 0; index < items.length; index++) {
callback(items[index]);
}
Expand All @@ -21,21 +21,26 @@ function forEach(items, callback) {

To test this function, we can use a mock function, and inspect the mock's state to ensure the callback is invoked as expected.

```javascript
```js title="forEach.test.js"
const forEach = require('./forEach');

const mockCallback = jest.fn(x => 42 + x);
forEach([0, 1], mockCallback);

// The mock function is called twice
expect(mockCallback.mock.calls.length).toBe(2);
test('forEach mock function', () => {
forEach([0, 1], mockCallback);

// The first argument of the first call to the function was 0
expect(mockCallback.mock.calls[0][0]).toBe(0);
// The mock function was called twice
expect(mockCallback.mock.calls).toHaveLength(2);

// The first argument of the second call to the function was 1
expect(mockCallback.mock.calls[1][0]).toBe(1);
// The first argument of the first call to the function was 0
expect(mockCallback.mock.calls[0][0]).toBe(0);

// The return value of the first call to the function was 42
expect(mockCallback.mock.results[0].value).toBe(42);
// The first argument of the second call to the function was 1
expect(mockCallback.mock.calls[1][0]).toBe(1);

// The return value of the first call to the function was 42
expect(mockCallback.mock.results[0].value).toBe(42);
});
```

## `.mock` property
Expand All @@ -60,7 +65,7 @@ These mock members are very useful in tests to assert how these functions get ca

```javascript
// The function was called exactly once
expect(someMockFunction.mock.calls.length).toBe(1);
expect(someMockFunction.mock.calls).toHaveLength(1);

// The first arg of the first call to the function was 'first arg'
expect(someMockFunction.mock.calls[0][0]).toBe('first arg');
Expand Down
31 changes: 18 additions & 13 deletions website/versioned_docs/version-29.0/MockFunctions.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ There are two ways to mock functions: Either by creating a mock function to use

Let's imagine we're testing an implementation of a function `forEach`, which invokes a callback for each item in a supplied array.

```javascript
function forEach(items, callback) {
```js title="forEach.js"
export function forEach(items, callback) {
for (let index = 0; index < items.length; index++) {
callback(items[index]);
}
Expand All @@ -21,21 +21,26 @@ function forEach(items, callback) {

To test this function, we can use a mock function, and inspect the mock's state to ensure the callback is invoked as expected.

```javascript
```js title="forEach.test.js"
const forEach = require('./forEach');

const mockCallback = jest.fn(x => 42 + x);
forEach([0, 1], mockCallback);

// The mock function is called twice
expect(mockCallback.mock.calls.length).toBe(2);
test('forEach mock function', () => {
forEach([0, 1], mockCallback);

// The first argument of the first call to the function was 0
expect(mockCallback.mock.calls[0][0]).toBe(0);
// The mock function was called twice
expect(mockCallback.mock.calls).toHaveLength(2);

// The first argument of the second call to the function was 1
expect(mockCallback.mock.calls[1][0]).toBe(1);
// The first argument of the first call to the function was 0
expect(mockCallback.mock.calls[0][0]).toBe(0);

// The return value of the first call to the function was 42
expect(mockCallback.mock.results[0].value).toBe(42);
// The first argument of the second call to the function was 1
expect(mockCallback.mock.calls[1][0]).toBe(1);

// The return value of the first call to the function was 42
expect(mockCallback.mock.results[0].value).toBe(42);
});
```

## `.mock` property
Expand All @@ -60,7 +65,7 @@ These mock members are very useful in tests to assert how these functions get ca

```javascript
// The function was called exactly once
expect(someMockFunction.mock.calls.length).toBe(1);
expect(someMockFunction.mock.calls).toHaveLength(1);

// The first arg of the first call to the function was 'first arg'
expect(someMockFunction.mock.calls[0][0]).toBe('first arg');
Expand Down
31 changes: 18 additions & 13 deletions website/versioned_docs/version-29.1/MockFunctions.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ There are two ways to mock functions: Either by creating a mock function to use

Let's imagine we're testing an implementation of a function `forEach`, which invokes a callback for each item in a supplied array.

```javascript
function forEach(items, callback) {
```js title="forEach.js"
export function forEach(items, callback) {
for (let index = 0; index < items.length; index++) {
callback(items[index]);
}
Expand All @@ -21,21 +21,26 @@ function forEach(items, callback) {

To test this function, we can use a mock function, and inspect the mock's state to ensure the callback is invoked as expected.

```javascript
```js title="forEach.test.js"
const forEach = require('./forEach');

const mockCallback = jest.fn(x => 42 + x);
forEach([0, 1], mockCallback);

// The mock function is called twice
expect(mockCallback.mock.calls.length).toBe(2);
test('forEach mock function', () => {
forEach([0, 1], mockCallback);

// The first argument of the first call to the function was 0
expect(mockCallback.mock.calls[0][0]).toBe(0);
// The mock function was called twice
expect(mockCallback.mock.calls).toHaveLength(2);

// The first argument of the second call to the function was 1
expect(mockCallback.mock.calls[1][0]).toBe(1);
// The first argument of the first call to the function was 0
expect(mockCallback.mock.calls[0][0]).toBe(0);

// The return value of the first call to the function was 42
expect(mockCallback.mock.results[0].value).toBe(42);
// The first argument of the second call to the function was 1
expect(mockCallback.mock.calls[1][0]).toBe(1);

// The return value of the first call to the function was 42
expect(mockCallback.mock.results[0].value).toBe(42);
});
```

## `.mock` property
Expand All @@ -60,7 +65,7 @@ These mock members are very useful in tests to assert how these functions get ca

```javascript
// The function was called exactly once
expect(someMockFunction.mock.calls.length).toBe(1);
expect(someMockFunction.mock.calls).toHaveLength(1);

// The first arg of the first call to the function was 'first arg'
expect(someMockFunction.mock.calls[0][0]).toBe('first arg');
Expand Down
Loading