Skip to content

Commit d8f8ba5

Browse files
authored
Merge pull request #1613 from andrewnicols/MDL-88762
Documentation for MDL-88762
2 parents d070750 + 2d3aba5 commit d8f8ba5

1 file changed

Lines changed: 66 additions & 23 deletions

File tree

‎docs/guides/javascript/react/testing.md‎

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,47 @@ describe('getString', () => {
107107
});
108108
```
109109

110-
## Mocking AMD modules
110+
## Mocking
111+
112+
Mocking of class and module dependencies of your unit under test is strongly encouraged. In some cases it is mandatory.
113+
114+
Some functionality, such as the ability to mock strings and AMD modules, is provided as part of the Moodle core and these mocks are reset between tests.
115+
116+
You should not need to clean mocks up manually. Each test starts with a fresh state.
117+
118+
You can clear up any additional test state within your test file using:
119+
120+
- `beforeEach()`
121+
- `afterEach()`
122+
123+
See the [Jest Setup and Teardown](https://jestjs.io/docs/setup-teardown) documentation for further information.
124+
125+
### Mocking strings
126+
127+
Because of the way in which Moodle's string module fetches strings using a web service, all strings are mocked to a standard value of:
128+
129+
```
130+
[identifier, component]
131+
```
132+
133+
For cases where your tests expect a specific string value, you can mock values using the global `mockString` method:
134+
135+
```typescript
136+
describe('@moodle/lms/mod_example/Example', () => {
137+
beforeEach(() => {
138+
mockString('dofabuluousthings', 'more_example', 'Do something fabulous!!!');
139+
});
140+
it('Renders an Example component', async () => {
141+
await act(async() => {
142+
render(<Example />);
143+
});
144+
145+
expect(screen.getByText('Do something fabulous!!!')).toBeInTheDocument();
146+
});
147+
});
148+
```
149+
150+
### Mocking AMD modules
111151

112152
AMD modules (anything loaded via `requirejs`) **cannot** run inside Jest. The Jest module system and the AMD loader are completely separate environments, so `requirejs`, `M`, jQuery, and other Moodle globals are not available.
113153

@@ -130,9 +170,9 @@ describe('my component', () => {
130170
});
131171
```
132172

133-
:::note
173+
:::danger[Unmocked AMD modules]
134174

135-
If code under test calls `requireAsync` or `requireManyAsync` with a module that has not been registered via `mockAmdModule`, the test will throw:
175+
If code under test calls `requireAsync` or `requireManyAsync` with a module that has not been registered using `mockAmdModule` then the test will throw an error:
136176

137177
```
138178
Error: Unexpected call to requireAsync with module name: core/notification
@@ -142,37 +182,40 @@ This is intentional: missing mocks produce a hard failure rather than silent wro
142182

143183
:::
144184

145-
### Registrations reset between tests
185+
## Handling redirects
146186

147-
Mocks and test fixtures, such as the AMD module map, and the string map, are cleared between each test using the `afterEach` notation.
148-
149-
You do not need to clean up manually. Each test starts with a fresh state.
150-
151-
You can clear up any additional test state within your test file using:
152-
153-
- `beforeEach()`
154-
- `afterEach()`
187+
If your code causes the page to redirect, then it must use the `@moodle/lms/core/location` module's `redirect` method, for example:
155188

156-
See the [Jest Setup and Teardown](https://jestjs.io/docs/setup-teardown) documentation for further information.
189+
```typescript
190+
import {redirect} from '@moodle/lms/core/location';
157191

158-
## Mocking language strings
192+
export default function () {
193+
redirect('https://example.com');
194+
}
195+
```
159196

160-
`mockString(identifier, component, resolved)` registers a resolved value for a specific `(identifier, component)` pair. This delegates to the default `core/str` mock that is already registered in `.jest/globalSetup.ts`.
197+
Moodle automatically mocks the `redirect` function and allows you to specify the expected value before calling your method using the `expectRedirect()` helper:
161198

162199
```typescript
163-
mockString('submit', 'core', 'Submit');
164-
mockString('cancel', 'core', 'Cancel');
200+
import Example from '@moodle/lms/mod_example/Example';
201+
202+
describe('@moodle/lms/mod_example/Example', () => {
203+
it('Redirects to the user documentation', () => {
204+
expectRedirect({urlContains: 'example.com'});
165205

166-
await expect(getString('submit', 'core')).resolves.toBe('Submit');
206+
Example();
207+
});
208+
});
167209
```
168210

169-
For any string that was not registered, the default mock returns `[identifier, component]`:
211+
The `expectRedirect` method accepts:
170212

171-
```typescript
172-
await expect(getString('other', 'core')).resolves.toBe('[other, core]');
173-
```
213+
- a `url` parameter with an exact matching URL; or
214+
- a `urlContains` parameter with a partial match.
215+
216+
If a redirect occurs without an `expectRedirect()` call, an Error will be thrown.
174217

175-
This default is useful for snapshot tests and assertions that only care whether a string key was requested, not its exact value.
218+
Expected redirects are reset between tests.
176219

177220
## Module path aliases
178221

0 commit comments

Comments
 (0)