@visiblevc/ember-api-mocks 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +9 -0
- package/README.md +366 -2
- package/addon-main.cjs +4 -0
- package/declarations/api.d.ts +46 -0
- package/declarations/api.d.ts.map +1 -0
- package/declarations/beacon.d.ts +3 -0
- package/declarations/beacon.d.ts.map +1 -0
- package/declarations/config.d.ts +23 -0
- package/declarations/config.d.ts.map +1 -0
- package/declarations/factories.d.ts +53 -0
- package/declarations/factories.d.ts.map +1 -0
- package/declarations/fetch-waiter.d.ts +7 -0
- package/declarations/fetch-waiter.d.ts.map +1 -0
- package/declarations/formats.d.ts +37 -0
- package/declarations/formats.d.ts.map +1 -0
- package/declarations/index.d.ts +8 -0
- package/declarations/index.d.ts.map +1 -0
- package/declarations/inflection.d.ts +6 -0
- package/declarations/inflection.d.ts.map +1 -0
- package/declarations/network.d.ts +15 -0
- package/declarations/network.d.ts.map +1 -0
- package/declarations/setup.d.ts +6 -0
- package/declarations/setup.d.ts.map +1 -0
- package/declarations/store.d.ts +6 -0
- package/declarations/store.d.ts.map +1 -0
- package/dist/index.js +1290 -0
- package/dist/index.js.map +1 -0
- package/package.json +149 -3
- package/src/api.ts +485 -0
- package/src/beacon.ts +25 -0
- package/src/config.ts +77 -0
- package/src/factories.ts +404 -0
- package/src/fetch-waiter.ts +161 -0
- package/src/formats.ts +231 -0
- package/src/index.ts +31 -0
- package/src/inflection.ts +53 -0
- package/src/network.ts +206 -0
- package/src/setup.ts +30 -0
- package/src/store.ts +214 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Visible
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,367 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @visiblevc/ember-api-mocks
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Request mocking and API-shaped test data for Ember apps.
|
|
4
|
+
|
|
5
|
+
- Requests are intercepted in the page by [MSW](https://mswjs.io) interceptors that patch `fetch` and `XMLHttpRequest`. There is no service worker, nothing to register, and no `public/mockServiceWorker.js`.
|
|
6
|
+
- Factories build the JSON your API sends: ActiveModel, REST or JSON:API, with your key casing and relationship ids. Mocks serve that JSON, so the app parses real responses through its own adapters and serializers.
|
|
7
|
+
- Component and unit tests push the same JSON into the store with `store.pushPayload`. Nothing reads or writes ember-data internals.
|
|
8
|
+
- A request the test did not mock fails the test and says which mock came close.
|
|
9
|
+
|
|
10
|
+
```js
|
|
11
|
+
import { api, buildList, setupApplicationTest } from '@visiblevc/ember-api-mocks';
|
|
12
|
+
|
|
13
|
+
module('Acceptance | contacts', function (hooks) {
|
|
14
|
+
setupApplicationTest(hooks);
|
|
15
|
+
|
|
16
|
+
test('renames a contact', async function (assert) {
|
|
17
|
+
const contacts = buildList('contact', 2, { company_id: 1 });
|
|
18
|
+
api.query('contact', contacts, { meta: { total: 2 } });
|
|
19
|
+
const update = api.update(contacts[0]).match({ contact: { name: 'Ada' } });
|
|
20
|
+
|
|
21
|
+
await visit('/contacts');
|
|
22
|
+
await fillIn('[data-test-name]', 'Ada');
|
|
23
|
+
await click('[data-test-save]');
|
|
24
|
+
|
|
25
|
+
assert.strictEqual(update.timesCalled, 1);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Compatibility
|
|
31
|
+
|
|
32
|
+
CI runs the addon's tests against:
|
|
33
|
+
|
|
34
|
+
| | Versions |
|
|
35
|
+
| --- | --- |
|
|
36
|
+
| ember-source | 5.8, 5.12, 6.4, 6.12, 7.x (latest, beta, alpha) |
|
|
37
|
+
| ember-data | 4.12 (with ember-data-model-fragments 9), 5.8, latest 5.x |
|
|
38
|
+
| Builds | Vite with Embroider (compat mode for the ember-source 5.x and ember-data 4.12 runs) |
|
|
39
|
+
| Node | 22.12 or later (required by MSW 3) |
|
|
40
|
+
|
|
41
|
+
Classic ember-cli apps with ember-auto-import 2 work too, but CI does not build one. ember-data is optional: `api` and `build` work without it, `seed` needs it.
|
|
42
|
+
|
|
43
|
+
Peer dependencies: `qunit` 2.17+, `ember-qunit` 8+, `@ember/test-helpers` 3+, `@ember/test-waiters` 3+. Optional: `ember-data` 4.12+, `ember-inflector` 4+, `@ember-data/request-utils` 5.3+, `ember-data-model-fragments` 9+.
|
|
44
|
+
|
|
45
|
+
## Installation
|
|
46
|
+
|
|
47
|
+
```sh
|
|
48
|
+
pnpm add -D @visiblevc/ember-api-mocks
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
It is a v2 addon. Classic apps need `ember-auto-import` 2.
|
|
52
|
+
|
|
53
|
+
Configure it once in `tests/test-helper.js`, before `start()`:
|
|
54
|
+
|
|
55
|
+
```js
|
|
56
|
+
import { activeModel, configure } from '@visiblevc/ember-api-mocks';
|
|
57
|
+
import config from 'my-app/config/environment';
|
|
58
|
+
|
|
59
|
+
configure({
|
|
60
|
+
apiHost: config.API_HOST,
|
|
61
|
+
format: activeModel(),
|
|
62
|
+
});
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Then use this package's setup functions instead of ember-qunit's:
|
|
66
|
+
|
|
67
|
+
```js
|
|
68
|
+
import {
|
|
69
|
+
setupApplicationTest,
|
|
70
|
+
setupRenderingTest,
|
|
71
|
+
setupTest,
|
|
72
|
+
} from '@visiblevc/ember-api-mocks';
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
If your app has its own wrappers in `tests/helpers`, call these from them.
|
|
76
|
+
|
|
77
|
+
### `configure` options
|
|
78
|
+
|
|
79
|
+
| Option | Default | |
|
|
80
|
+
| --- | --- | --- |
|
|
81
|
+
| `apiHost` | the page origin | Base URL for paths that start with `/`. May include a path (`https://api.example.com/v2`). |
|
|
82
|
+
| `namespace` | `''` | Prefix for type shortcut URLs only (`api/v1`). |
|
|
83
|
+
| `format` | `jsonApi()` | Default wire format. A factory can override it. |
|
|
84
|
+
| `origins` | `[]` | More origins that count as your app (a CDN, a second API). |
|
|
85
|
+
| `bypass` | Percy's local server | Origins that always reach the network untouched. The default is the address `@percy/sdk-utils` reports (`http://localhost:5338` unless Percy says otherwise); setting `bypass` replaces it. |
|
|
86
|
+
| `beacons` | `false` | Send `navigator.sendBeacon` through `fetch` so mocks can answer it. |
|
|
87
|
+
|
|
88
|
+
`configure` merges with earlier calls. Settings are read when the first test runs.
|
|
89
|
+
|
|
90
|
+
### Formats
|
|
91
|
+
|
|
92
|
+
The format decides the document shape, the key check on overrides, how relationships are written, the default collection URL and the update method.
|
|
93
|
+
|
|
94
|
+
ActiveModel (Rails with active_model_serializers and similar): underscored root keys, `company_id` and `tag_ids`, sideloaded arrays, `PUT`.
|
|
95
|
+
|
|
96
|
+
```js
|
|
97
|
+
configure({ format: activeModel() });
|
|
98
|
+
// GET /contacts/1 -> { contact: { id: 1, name: 'Ada', company_id: 3 }, companies: [...] }
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
REST (ember-data's `RESTSerializer`): relationship keys are the relationship names, root keys follow `keys` (`'camel'`, `'snake'` or `'dash'`), `PUT`. The default path is the stock `RESTAdapter` one (`/sharedDecks`), so apps whose adapters underscore or dasherize pass `path`:
|
|
102
|
+
|
|
103
|
+
```js
|
|
104
|
+
import { underscore } from '@ember/string';
|
|
105
|
+
import { pluralize } from 'ember-inflector';
|
|
106
|
+
|
|
107
|
+
configure({
|
|
108
|
+
format: rest({ keys: 'snake', path: (type) => `/${pluralize(underscore(type))}` }),
|
|
109
|
+
});
|
|
110
|
+
// GET /shared_decks/1 -> { shared_deck: { id: 1, title: 'Q3', owner: 2 } }
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
JSON:API: `{ data, included }`, plural dasherized types, string ids, `PATCH`. A relationship's `data` is written only when the related records are in the document, as jsonapi-resources does. `seed` always writes it.
|
|
114
|
+
|
|
115
|
+
```js
|
|
116
|
+
configure({ format: jsonApi() });
|
|
117
|
+
// GET /investors/1 -> { data: { id: '1', type: 'investors', attributes: {...}, relationships: {...} }, included: [...] }
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
A factory can use another format or a fixed URL:
|
|
121
|
+
|
|
122
|
+
```js
|
|
123
|
+
defineFactory('connect-investor', {
|
|
124
|
+
format: jsonApi(),
|
|
125
|
+
url: 'https://connect.example.com/api/investors',
|
|
126
|
+
});
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Factories
|
|
130
|
+
|
|
131
|
+
Define factories once, for example in `tests/factories/*.js` imported from `tests/test-helper.js`. Keys are the API's keys, in the format's casing.
|
|
132
|
+
|
|
133
|
+
```js
|
|
134
|
+
import {
|
|
135
|
+
belongsTo,
|
|
136
|
+
defineFactory,
|
|
137
|
+
hasMany,
|
|
138
|
+
sequence,
|
|
139
|
+
} from '@visiblevc/ember-api-mocks';
|
|
140
|
+
|
|
141
|
+
defineFactory('company', {
|
|
142
|
+
default: {
|
|
143
|
+
name: (company) => `Company #${company.id}`,
|
|
144
|
+
slug: sequence((n) => `company-${n}`),
|
|
145
|
+
settings: { currency: 'USD' },
|
|
146
|
+
},
|
|
147
|
+
traits: {
|
|
148
|
+
withSubscription: {
|
|
149
|
+
subscriptions: hasMany('subscription', 1, 'trialing'),
|
|
150
|
+
},
|
|
151
|
+
archived: { archived_at: '2024-01-01T00:00:00Z' },
|
|
152
|
+
shouting: (company) => {
|
|
153
|
+
company.name = company.name.toUpperCase();
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
transient: { role: 'admin' },
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
defineFactory('subscription', {
|
|
160
|
+
default: { plan_id: 'basic' },
|
|
161
|
+
traits: { trialing: { status: 'trialing' } },
|
|
162
|
+
relationships: { company: belongsTo('company') },
|
|
163
|
+
});
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
- `build(name, ...traits, overrides)` returns a plain JSON record with an `id`. `buildList(name, 3, ...traits, overrides)` returns three; without a count, each argument describes one record: `buildList('user', { name: 'A' }, ['admin', { name: 'B' }])`.
|
|
167
|
+
- Ids count up from 1 per factory and restart for every test. An explicit id (`{ id: 5 }` or `{ id: '5' }`) moves the counter past it. For string or uuid ids, set `default.id` to a `sequence((n) => ...)`.
|
|
168
|
+
- Defaults are copied per record. A function value is called with the record once the other values are set.
|
|
169
|
+
- A `sequence` advances only when the default value is used. For values that follow the id, as in Mirage's `(i) => ...`, use a function of the record: `` name: ({ id }) => `User ${id}` ``.
|
|
170
|
+
- `transient` values are options, not attributes. Read them in `afterSeed`.
|
|
171
|
+
- An override key in the wrong case for the format throws (`planId` for ActiveModel, `plan_id` for JSON:API). The serializer would ignore it and the test would run on the wrong data.
|
|
172
|
+
|
|
173
|
+
### Relationships
|
|
174
|
+
|
|
175
|
+
In `default` or a trait, `belongsTo(type, ...)` and `hasMany(type, count, ...)` build the related records, write their ids, and remember them so they are sideloaded (or `included`) with the parent:
|
|
176
|
+
|
|
177
|
+
```js
|
|
178
|
+
const company = build('company', 'withSubscription');
|
|
179
|
+
payload('company', company);
|
|
180
|
+
// { company: { id: 1, name: 'Company #1', ..., subscription_ids: [1] },
|
|
181
|
+
// subscriptions: [{ id: 1, plan_id: 'basic', status: 'trialing' }] }
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
In `relationships`, they only declare the type, so overrides can pass an id, a list of ids, a built record or, for a polymorphic relationship, `{ type, id }`:
|
|
185
|
+
|
|
186
|
+
```js
|
|
187
|
+
build('subscription', { company: 3 }); // company_id: 3
|
|
188
|
+
build('subscription', { company }); // company_id: 1, company sideloaded
|
|
189
|
+
build('note', { subject: { type: 'person', id: 4 } }); // JSON:API: { type: 'people', id: '4' }
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
Declared relationships also apply to values assigned after `build`: `subscription.company = company` serves `company_id` and sideloads the company. Without a declaration, write the wire key yourself: `{ company_id: 3 }`. A built record under an undeclared key throws. JSON:API keeps relationships apart from attributes, so declare every relationship there; `seed` throws on an undeclared one.
|
|
193
|
+
|
|
194
|
+
`fragment(type, ...)` builds a nested object without an id, for embedded objects and ember-data-model-fragments.
|
|
195
|
+
|
|
196
|
+
### Payloads
|
|
197
|
+
|
|
198
|
+
- `payload(type, recordOrList, extra)` returns the document a request would get. `extra` adds top-level keys such as `{ meta }` or `{ links }`.
|
|
199
|
+
- `sideload(record, ...others)` attaches more records to a record's documents.
|
|
200
|
+
- `mergePayloads(...payloads)` concatenates arrays under the same key.
|
|
201
|
+
- `factoryOf(record)` returns the factory name of a built record.
|
|
202
|
+
|
|
203
|
+
## Component and unit tests: `seed`
|
|
204
|
+
|
|
205
|
+
`seed`, `seedList` and `seedRecord` push factory JSON through your serializers with `store.pushPayload` and return the models.
|
|
206
|
+
|
|
207
|
+
```js
|
|
208
|
+
import { seed, setupRenderingTest } from '@visiblevc/ember-api-mocks';
|
|
209
|
+
|
|
210
|
+
test('shows the plan', async function (assert) {
|
|
211
|
+
this.company = seed('company', 'withSubscription', { name: 'Acme' });
|
|
212
|
+
|
|
213
|
+
await render(hbs`<CompanyCard @company={{this.company}} />`);
|
|
214
|
+
|
|
215
|
+
assert.dom('[data-test-name]').hasText('Acme');
|
|
216
|
+
});
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
- `seed` throws on an override key the model's serializer would not read, such as `company` where the API sends `company_id`.
|
|
220
|
+
- `seedRecord(build(...))` seeds a record built earlier. `seedList` takes `buildList`'s arguments.
|
|
221
|
+
- Factories can define `beforeSeed(record, store)` and `afterSeed(model, transient)` hooks. They run for `seed` only.
|
|
222
|
+
- In TypeScript, pass the type: `seed<Company>('company')`, `build<CompanyJson>('company')`.
|
|
223
|
+
- For a new, unsaved record, use `store.createRecord`.
|
|
224
|
+
- With ember-data-model-fragments installed, `seed` on a fragment factory returns a fragment.
|
|
225
|
+
|
|
226
|
+
## Acceptance tests: `build` and `api`
|
|
227
|
+
|
|
228
|
+
Acceptance tests build JSON, mock the requests the page makes, and let the app load it.
|
|
229
|
+
|
|
230
|
+
### Type shortcuts
|
|
231
|
+
|
|
232
|
+
| Call | Request | Default response |
|
|
233
|
+
| --- | --- | --- |
|
|
234
|
+
| `api.query('contact', records, extra)` | `GET /contacts` | the list, its sideloads and `extra` |
|
|
235
|
+
| `api.find(record, extra)` | `GET /contacts/1` | the record and its sideloads |
|
|
236
|
+
| `api.find('contact', id?, extra)` | `GET /contacts/:id` | `{ contact: { id } }` |
|
|
237
|
+
| `api.create('contact', attrs)` | `POST /contacts` | `attrs` with the id from the body or the next factory id |
|
|
238
|
+
| `api.update(record, attrs)` or `api.update('contact', id?, attrs)` | `PUT` or `PATCH /contacts/:id` | `{ id, ...attrs }` |
|
|
239
|
+
| `api.destroy(record)` or `api.destroy('contact', id?)` | `DELETE /contacts/:id` | 204 |
|
|
240
|
+
|
|
241
|
+
URLs come from the factory's `url`, or `namespace` plus the format's path. `find`, `update` and `destroy` accept a record from `build` or `seed`; without an id they answer any id. A record mocks its own URL (`/contacts/1`), so `lastCall.params.id` is undefined; pass the type instead (`api.update('contact')`) when a test checks the id. Payloads are put together when the request arrives, so later changes to a built record are served. Every shortcut also takes a function of the call in place of the default response.
|
|
242
|
+
|
|
243
|
+
### Explicit paths
|
|
244
|
+
|
|
245
|
+
```js
|
|
246
|
+
api.get('/users/me', { user: build('user') });
|
|
247
|
+
api.post('/files/sign', reply(201, { url: 'https://example.com/upload' }));
|
|
248
|
+
api.put('/reports/:id', ({ params, body }) => ({
|
|
249
|
+
report: { id: params.id, ...body.report },
|
|
250
|
+
}));
|
|
251
|
+
api.patch('/settings', null);
|
|
252
|
+
api.delete('https://auth.example.com/sessions', null);
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
Paths that start with `/` are joined to `apiHost`, without `namespace`; absolute URLs are used as is. Any other path throws. A response is an object, a function of the call (sync or async), a `Response` (`reply(status, body)`, `reply(status)` for an empty body, or MSW's `HttpResponse`, re-exported here), or `null` for a 204. A model from `seed` inside a plain response is served as its source JSON.
|
|
256
|
+
|
|
257
|
+
### Handles
|
|
258
|
+
|
|
259
|
+
Every call returns a handle:
|
|
260
|
+
|
|
261
|
+
```js
|
|
262
|
+
const create = api
|
|
263
|
+
.create('contact', { name: 'Ada' })
|
|
264
|
+
.match({ contact: { name: 'Ada', tag_ids: ['1', '2'] } })
|
|
265
|
+
.withHeaders({ Authorization: (value) => value?.startsWith('Bearer ') });
|
|
266
|
+
|
|
267
|
+
await click('[data-test-save]');
|
|
268
|
+
|
|
269
|
+
assert.strictEqual(create.timesCalled, 1);
|
|
270
|
+
assert.deepEqual(create.lastCall.body.contact.tag_ids, ['1', '2']);
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
- `timesCalled`, `calls` and `lastCall`. A call has `method`, `url`, `params`, `query`, `body` (parsed JSON or form data) and `headers`.
|
|
274
|
+
- `.match(body)` answers only when the body contains these keys. Objects match as subsets, arrays in full, values compare as strings, a key must be sent (`null` and `''` too), and a function is called with the value.
|
|
275
|
+
- `.withQuery(params)` answers only the exact query: every param present, nothing extra, and `ids[]=1` is an array, not `1`. A repeated `ids=1&ids=2` is an array too. `.withQuery(params, { partial: true })` ignores other params.
|
|
276
|
+
- `.withHeaders(headers)` matches header names in any case. A value is a string, a predicate, or `null` for absent.
|
|
277
|
+
- `.returns(...)` changes the response. On a shortcut it takes the shortcut's arguments: `query.returns(list, { meta })`, `find.returns(record, extra)`, `create.returns(attrs)`.
|
|
278
|
+
- `.fails(status = 500, body = {})` answers with an error.
|
|
279
|
+
- `.remove()` takes the mock away.
|
|
280
|
+
|
|
281
|
+
The newest mock for a URL answers first. A mock whose guard does not fit is skipped and the next one is tried. `api.requests` lists every answered call and `api.lastRequest(method, path)` finds one.
|
|
282
|
+
|
|
283
|
+
### Unhandled requests
|
|
284
|
+
|
|
285
|
+
A request no mock answers gets a 404. For the page origin, `apiHost` and `origins`, the test also fails with `No handler for GET https://...` plus what any near-miss guard expected and received. Non-http URLs (`data:`, `blob:`) and `bypass` origins always go out untouched. To let one request reach the network, answer it with `passthrough()`: `api.get(url, () => passthrough())`.
|
|
286
|
+
|
|
287
|
+
### Waiting for requests
|
|
288
|
+
|
|
289
|
+
Mocked requests register with a test waiter, so `await settled()`, `click`, `visit` and the other helpers wait for them, including a raw `fetch` from a service. A `fetch` stays pending until the app has read the response body with `json()`, `text()` or another reader, or for two macrotasks if it never reads one. Reading `response.body` as a stream is not tracked. After each test, the setup functions wait up to one second for requests and other test waiters still pending, and fail the test if any remain. Mocked responses arrive in real time, also under fake timers.
|
|
290
|
+
|
|
291
|
+
### Beacons
|
|
292
|
+
|
|
293
|
+
With `configure({ beacons: true })`, `navigator.sendBeacon` goes through `fetch` while mocking is on, so a mock answers it and `handle.lastCall.body` holds the parsed body. Set it in `tests/test-helper.js`; it takes effect when a test starts.
|
|
294
|
+
|
|
295
|
+
## Migrating
|
|
296
|
+
|
|
297
|
+
### From ember-data-factory-guy
|
|
298
|
+
|
|
299
|
+
| factory-guy | ember-api-mocks |
|
|
300
|
+
| --- | --- |
|
|
301
|
+
| `make('user', 'admin', { name })` | `seed('user', 'admin', { name })` |
|
|
302
|
+
| `makeList('user', 2)` | `seedList('user', 2)` |
|
|
303
|
+
| `build('user')`, `buildList('user', 2)` | `build`, `buildList` (plain wire JSON) |
|
|
304
|
+
| `mockFindRecord(user)` | `api.find(user)` |
|
|
305
|
+
| `mockFindAll('user')` | `api.query('user', records)` |
|
|
306
|
+
| `mockQuery('user', { page: 1 }).returns({ models })` | `api.query('user', records).withQuery({ page: 1 })` |
|
|
307
|
+
| `mockCreate('user').match(attrs).returns({ attrs })` | `api.create('user', attrs).match(wireBody)` |
|
|
308
|
+
| `mockUpdate(user).fails({ status: 422, response })` | `api.update(user).fails(422, body)` |
|
|
309
|
+
| `mockDelete(user)` | `api.destroy(user)` |
|
|
310
|
+
| `mock({ type: 'GET', url, responseText })` | `api.get(url, response)` |
|
|
311
|
+
| `.timesCalled` | `.timesCalled`, `.calls`, `.lastCall` |
|
|
312
|
+
| `setupFactoryGuy(this)` | `setupTest(hooks)` and the other setup functions |
|
|
313
|
+
|
|
314
|
+
`match` sees the request body as sent, in the API's keys, not model attributes.
|
|
315
|
+
|
|
316
|
+
### From Mirage
|
|
317
|
+
|
|
318
|
+
| Mirage | ember-api-mocks |
|
|
319
|
+
| --- | --- |
|
|
320
|
+
| `server.create('user', attrs)` | `build('user', attrs)` plus a mock that serves it |
|
|
321
|
+
| `server.createList('user', 3)` | `buildList('user', 3)` |
|
|
322
|
+
| `this.get('/users')` shorthand | `api.query('user', records)` |
|
|
323
|
+
| `this.get('/users/:id', (schema, request) => ...)` | `api.get('/users/:id', ({ params, query, body }) => ...)` |
|
|
324
|
+
| `schema.users.where(...)`, `.update(...)` | an array of built records kept in the test, read and changed by responders |
|
|
325
|
+
| `this.normalizedRequestAttrs()` | `call.body` |
|
|
326
|
+
| `new Response(422, {}, errors)` | `reply(422, errors)` |
|
|
327
|
+
| `server.pretender.handledRequests` | `api.requests`, `handle.calls` |
|
|
328
|
+
| factory `afterCreate` | a trait function, or `afterSeed` for `seed` |
|
|
329
|
+
| `this.passthrough()` | `passthrough()` from a responder |
|
|
330
|
+
|
|
331
|
+
Where Mirage kept state in its ORM, keep built records in the test and let responders read and change them:
|
|
332
|
+
|
|
333
|
+
```js
|
|
334
|
+
const users = buildList('user', 2);
|
|
335
|
+
api.query('user', users);
|
|
336
|
+
const create = api.post('/users', ({ body }) => {
|
|
337
|
+
const user = build('user', body.user);
|
|
338
|
+
users.push(user);
|
|
339
|
+
return payload('user', user);
|
|
340
|
+
});
|
|
341
|
+
api.destroy('user', ({ params }) => {
|
|
342
|
+
users.splice(users.findIndex(({ id }) => String(id) === params.id), 1);
|
|
343
|
+
return null;
|
|
344
|
+
});
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
## Things to know
|
|
348
|
+
|
|
349
|
+
- Only `fetch` and `XMLHttpRequest` are intercepted. `<img>`, CSS, `<script>`, iframes and WebSockets load for real.
|
|
350
|
+
- Code that captured `window.fetch` before the interceptors patched it bypasses the mocks.
|
|
351
|
+
- Mock the request instead of stubbing `window.fetch` (for example with `sinon.stub(window, 'fetch')`). A stub replaces the mocking wrapper for that test; one restored after teardown leaves a stale wrapper behind, which passes requests straight to the original `fetch` and is unwrapped when the next test starts.
|
|
352
|
+
- Query values arrive as strings, and an exact `withQuery` must list empty params too (`?tag=` is `{ tag: '' }`).
|
|
353
|
+
- Built ids are numbers (JSON:API documents stringify them) and request bodies carry strings. Compare with `String(record.id)`.
|
|
354
|
+
- A record in a response must come from `build` or `seed`. A record from `store.createRecord` throws; respond with JSON.
|
|
355
|
+
- Requests sent together (`Promise.all`) are matched after their bodies are parsed, so their handlers can run in either order. Assert on each handle's `calls`, not on the order of `api.requests`.
|
|
356
|
+
- `api.query` answers the same payload whatever the query. For paging or search, use `api.get` with a function that returns `payload(type, records.slice(...), { meta })`.
|
|
357
|
+
- Pluralization comes from `@ember-data/request-utils` 5.3 or later when the app depends on it directly, then `ember-inflector`. Register custom rules where your serializers read them. Without either package the rule is a plain `s`, so give a factory with an irregular plural a `url`.
|
|
358
|
+
- `seed` uses `store.pushPayload`, which ember-data 5 deprecates.
|
|
359
|
+
- Interception uses `defineNetwork` from `msw/experimental`, so `msw` is pinned to an exact version and updated deliberately.
|
|
360
|
+
|
|
361
|
+
## Contributing
|
|
362
|
+
|
|
363
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
364
|
+
|
|
365
|
+
## License
|
|
366
|
+
|
|
367
|
+
MIT
|
package/addon-main.cjs
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { Attrs } from './formats.ts';
|
|
2
|
+
export type Json = any;
|
|
3
|
+
export interface Call {
|
|
4
|
+
method: string;
|
|
5
|
+
url: string;
|
|
6
|
+
params: Record<string, string | readonly string[] | undefined>;
|
|
7
|
+
query: Json;
|
|
8
|
+
body: Json;
|
|
9
|
+
headers: Headers;
|
|
10
|
+
}
|
|
11
|
+
type Responder = (call: Call) => unknown;
|
|
12
|
+
type ResponseValue = Responder | Response | object | null | undefined;
|
|
13
|
+
type HeaderValue = string | number | null | ((value: string | null) => unknown);
|
|
14
|
+
type Arg = ResponseValue | string | number;
|
|
15
|
+
export interface Handle {
|
|
16
|
+
readonly calls: Call[];
|
|
17
|
+
readonly timesCalled: number;
|
|
18
|
+
readonly lastCall: Call;
|
|
19
|
+
returns(...args: Arg[]): Handle;
|
|
20
|
+
fails(status?: number, body?: unknown): Handle;
|
|
21
|
+
match(body: object | ((body: Json) => unknown)): Handle;
|
|
22
|
+
withQuery(query: Attrs, options?: {
|
|
23
|
+
partial?: boolean;
|
|
24
|
+
}): Handle;
|
|
25
|
+
withHeaders(headers: Record<string, HeaderValue>): Handle;
|
|
26
|
+
remove(): void;
|
|
27
|
+
}
|
|
28
|
+
export declare function absoluteUrl(path: string): string;
|
|
29
|
+
export declare function reply(status: number, body?: unknown): Response;
|
|
30
|
+
export declare function clearRequests(): void;
|
|
31
|
+
export declare const api: {
|
|
32
|
+
readonly requests: Call[];
|
|
33
|
+
lastRequest(method: string, path: string): Call | undefined;
|
|
34
|
+
get: (path: string, response?: ResponseValue) => Handle;
|
|
35
|
+
post: (path: string, response?: ResponseValue) => Handle;
|
|
36
|
+
put: (path: string, response?: ResponseValue) => Handle;
|
|
37
|
+
patch: (path: string, response?: ResponseValue) => Handle;
|
|
38
|
+
delete: (path: string, response?: ResponseValue) => Handle;
|
|
39
|
+
find(typeOrRecord: string | object, ...args: Arg[]): Handle;
|
|
40
|
+
query(type: string, records?: unknown[], extra?: Attrs): Handle;
|
|
41
|
+
create(type: string, attrs?: Attrs | Responder): Handle;
|
|
42
|
+
update(typeOrRecord: string | object, ...args: Arg[]): Handle;
|
|
43
|
+
destroy(typeOrRecord: string | object, ...args: Arg[]): Handle;
|
|
44
|
+
};
|
|
45
|
+
export {};
|
|
46
|
+
//# sourceMappingURL=api.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,KAAK,EAAe,MAAM,cAAc,CAAC;AAWvD,MAAM,MAAM,IAAI,GAAG,GAAG,CAAC;AAEvB,MAAM,WAAW,IAAI;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,MAAM,EAAE,GAAG,SAAS,CAAC,CAAC;IAC/D,KAAK,EAAE,IAAI,CAAC;IACZ,IAAI,EAAE,IAAI,CAAC;IACX,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,KAAK,SAAS,GAAG,CAAC,IAAI,EAAE,IAAI,KAAK,OAAO,CAAC;AACzC,KAAK,aAAa,GAAG,SAAS,GAAG,QAAQ,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC;AACtE,KAAK,WAAW,GAAG,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,CAAC,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,KAAK,OAAO,CAAC,CAAC;AAChF,KAAK,GAAG,GAAG,aAAa,GAAG,MAAM,GAAG,MAAM,CAAC;AAY3C,MAAM,WAAW,MAAM;IACrB,QAAQ,CAAC,KAAK,EAAE,IAAI,EAAE,CAAC;IACvB,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,QAAQ,EAAE,IAAI,CAAC;IACxB,OAAO,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,MAAM,CAAC;IAChC,KAAK,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,GAAG,MAAM,CAAC;IAC/C,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,CAAC,CAAC,IAAI,EAAE,IAAI,KAAK,OAAO,CAAC,GAAG,MAAM,CAAC;IACxD,SAAS,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC,EAAE;QAAE,OAAO,CAAC,EAAE,OAAO,CAAA;KAAE,GAAG,MAAM,CAAC;IACjE,WAAW,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,GAAG,MAAM,CAAC;IAC1D,MAAM,IAAI,IAAI,CAAC;CAChB;AAuJD,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAWhD;AAiKD,wBAAgB,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,GAAG,QAAQ,CAI9D;AAED,wBAAgB,aAAa,IAAI,IAAI,CAEpC;AAED,eAAO,MAAM,GAAG;uBACE,IAAI,EAAE;wBAIF,MAAM,QAAQ,MAAM,GAAG,IAAI,GAAG,SAAS;gBAS/C,MAAM,aAAa,aAAa;iBAE/B,MAAM,aAAa,aAAa;gBAEjC,MAAM,aAAa,aAAa;kBAE9B,MAAM,aAAa,aAAa;mBAE/B,MAAM,aAAa,aAAa;uBAG5B,MAAM,GAAG,MAAM,WAAW,GAAG,EAAE,GAAG,MAAM;gBAqB/C,MAAM,YAAW,OAAO,EAAE,UAAc,KAAK,GAAQ,MAAM;iBAc1D,MAAM,UAAS,KAAK,GAAG,SAAS,GAAQ,MAAM;yBAmBtC,MAAM,GAAG,MAAM,WAAW,GAAG,EAAE,GAAG,MAAM;0BAiBvC,MAAM,GAAG,MAAM,WAAW,GAAG,EAAE,GAAG,MAAM;CAI/D,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"beacon.d.ts","sourceRoot":"","sources":["../src/beacon.ts"],"names":[],"mappings":"AAEA,wBAAgB,iBAAiB,IAAI,IAAI,CAexC;AAED,wBAAgB,gBAAgB,IAAI,IAAI,CAKvC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type Format } from './formats.ts';
|
|
2
|
+
export interface Options {
|
|
3
|
+
apiHost?: string;
|
|
4
|
+
namespace?: string;
|
|
5
|
+
origins?: string[];
|
|
6
|
+
format?: Format;
|
|
7
|
+
beacons?: boolean;
|
|
8
|
+
bypass?: string[];
|
|
9
|
+
}
|
|
10
|
+
export interface Settings {
|
|
11
|
+
apiBase: string;
|
|
12
|
+
namespace: string;
|
|
13
|
+
appOrigins: string[];
|
|
14
|
+
format: Format;
|
|
15
|
+
beacons: boolean;
|
|
16
|
+
bypass: string[];
|
|
17
|
+
bypassPercy: boolean;
|
|
18
|
+
}
|
|
19
|
+
export declare function configure(next: Options): void;
|
|
20
|
+
export declare function resetConfiguration(): void;
|
|
21
|
+
export declare function percyOrigin(): string;
|
|
22
|
+
export declare function settings(): Settings;
|
|
23
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,KAAK,MAAM,EAAE,MAAM,cAAc,CAAC;AAEpD,MAAM,WAAW,OAAO;IACtB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;CACnB;AAED,MAAM,WAAW,QAAQ;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,WAAW,EAAE,OAAO,CAAC;CACtB;AAKD,wBAAgB,SAAS,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI,CAG7C;AAED,wBAAgB,kBAAkB,IAAI,IAAI,CAGzC;AAID,wBAAgB,WAAW,IAAI,MAAM,CAWpC;AAMD,wBAAgB,QAAQ,IAAI,QAAQ,CAuBnC"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { Json } from './api.ts';
|
|
2
|
+
import type { Attrs, BuiltRecord, Format, RelationshipKind } from './formats.ts';
|
|
3
|
+
type Value = ((record: Json) => unknown) | object | string | number | boolean | null | undefined;
|
|
4
|
+
type Values = Record<string, Value>;
|
|
5
|
+
type Trait = Values | ((record: Json) => void);
|
|
6
|
+
type Overrides = Attrs;
|
|
7
|
+
type BuildArg = string | Overrides;
|
|
8
|
+
type ListArg = number | BuildArg | BuildArg[];
|
|
9
|
+
export interface FactoryDefinition {
|
|
10
|
+
default?: Values;
|
|
11
|
+
traits?: Record<string, Trait>;
|
|
12
|
+
transient?: Values;
|
|
13
|
+
relationships?: Record<string, Association>;
|
|
14
|
+
format?: Format;
|
|
15
|
+
url?: string;
|
|
16
|
+
beforeSeed?: (record: Json, store: Json) => void;
|
|
17
|
+
afterSeed?: (model: Json, transient: Json) => void;
|
|
18
|
+
}
|
|
19
|
+
interface Override {
|
|
20
|
+
key: string;
|
|
21
|
+
relationship: boolean;
|
|
22
|
+
}
|
|
23
|
+
declare class Sequence {
|
|
24
|
+
readonly generate: (n: number) => unknown;
|
|
25
|
+
constructor(generate: (n: number) => unknown);
|
|
26
|
+
}
|
|
27
|
+
export declare class Association {
|
|
28
|
+
readonly kind: RelationshipKind | 'fragment';
|
|
29
|
+
readonly type: string;
|
|
30
|
+
readonly args: ListArg[];
|
|
31
|
+
constructor(kind: RelationshipKind | 'fragment', type: string, args: ListArg[]);
|
|
32
|
+
}
|
|
33
|
+
export declare function nextId(name: string): number;
|
|
34
|
+
export declare function resetSequences(): void;
|
|
35
|
+
export declare function defineFactory(name: string, definition?: FactoryDefinition): void;
|
|
36
|
+
export declare function belongsTo(type: string, ...args: BuildArg[]): Association;
|
|
37
|
+
export declare function hasMany(type: string, ...args: ListArg[]): Association;
|
|
38
|
+
export declare function fragment(type: string, ...args: BuildArg[]): Association;
|
|
39
|
+
export declare function sequence(generate: (n: number) => unknown): Sequence;
|
|
40
|
+
export declare function definitionOf(name: string): FactoryDefinition & {
|
|
41
|
+
format: Format;
|
|
42
|
+
};
|
|
43
|
+
export declare function build<T extends object = BuiltRecord>(name: string, ...args: BuildArg[]): T;
|
|
44
|
+
export declare function buildList<T extends object = BuiltRecord>(name: string, ...args: unknown[]): T[];
|
|
45
|
+
export declare function factoryOf(record: unknown): string | undefined;
|
|
46
|
+
export declare function transientOf(record: BuiltRecord): Attrs;
|
|
47
|
+
export declare function overridesOf(record: BuiltRecord): Override[];
|
|
48
|
+
export declare function sideload<T extends object>(record: T, ...related: Array<object | null | undefined>): T;
|
|
49
|
+
export declare function documentFor(type: string, data: BuiltRecord | BuiltRecord[], extra: Attrs, seeding: boolean): Attrs;
|
|
50
|
+
export declare function payload(type: string, data: object | object[], extra?: Attrs): Attrs;
|
|
51
|
+
export declare function mergePayloads(...payloads: Attrs[]): Attrs;
|
|
52
|
+
export {};
|
|
53
|
+
//# sourceMappingURL=factories.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"factories.d.ts","sourceRoot":"","sources":["../src/factories.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const nativeSetTimeout: ((handler: TimerHandler, timeout?: number, ...arguments: any[]) => number) & typeof setTimeout;
|
|
2
|
+
export declare function afterTwoMacrotasks(callback: () => void): void;
|
|
3
|
+
export declare function unwrapStaleFetch(): void;
|
|
4
|
+
export declare function installFetchWaiter(original: typeof fetch, tracks: (url: string) => boolean): void;
|
|
5
|
+
export declare function endOpenResponses(): void;
|
|
6
|
+
export declare function removeFetchWaiter(): void;
|
|
7
|
+
//# sourceMappingURL=fetch-waiter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch-waiter.d.ts","sourceRoot":"","sources":["../src/fetch-waiter.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,gBAAgB,gGAAiC,CAAC;AAsB/D,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,IAAI,CAE7D;AA6DD,wBAAgB,gBAAgB,IAAI,IAAI,CAYvC;AAED,wBAAgB,kBAAkB,CAChC,QAAQ,EAAE,OAAO,KAAK,EACtB,MAAM,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,GAC/B,IAAI,CAwCN;AAED,wBAAgB,gBAAgB,IAAI,IAAI,CAEvC;AAED,wBAAgB,iBAAiB,IAAI,IAAI,CASxC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export type Attrs = Record<string, unknown>;
|
|
2
|
+
export type BuiltRecord = Attrs & {
|
|
3
|
+
id: unknown;
|
|
4
|
+
};
|
|
5
|
+
export type RelationshipKind = 'belongsTo' | 'hasMany';
|
|
6
|
+
export interface Relationship {
|
|
7
|
+
type: string;
|
|
8
|
+
kind: RelationshipKind;
|
|
9
|
+
}
|
|
10
|
+
export interface Related {
|
|
11
|
+
type: string;
|
|
12
|
+
record: BuiltRecord;
|
|
13
|
+
}
|
|
14
|
+
export interface DocumentContext {
|
|
15
|
+
seeding: boolean;
|
|
16
|
+
related(record: BuiltRecord): Related[];
|
|
17
|
+
relationships(record: BuiltRecord): Map<string, Relationship>;
|
|
18
|
+
}
|
|
19
|
+
export interface Format {
|
|
20
|
+
readonly updateMethod: 'put' | 'patch';
|
|
21
|
+
readonly separatesRelationships: boolean;
|
|
22
|
+
keyError(key: string): string | undefined;
|
|
23
|
+
relationshipKey(key: string, kind: RelationshipKind): string;
|
|
24
|
+
path(type: string): string;
|
|
25
|
+
document(type: string, data: BuiltRecord | BuiltRecord[], extra: Attrs, context: DocumentContext): Attrs;
|
|
26
|
+
idFromBody(type: string, body: unknown): unknown;
|
|
27
|
+
}
|
|
28
|
+
type KeyCase = 'snake' | 'camel' | 'dash';
|
|
29
|
+
export declare function activeModel(): Format;
|
|
30
|
+
export interface RestOptions {
|
|
31
|
+
keys?: KeyCase;
|
|
32
|
+
path?: (type: string) => string;
|
|
33
|
+
}
|
|
34
|
+
export declare function rest(options?: RestOptions): Format;
|
|
35
|
+
export declare function jsonApi(): Format;
|
|
36
|
+
export {};
|
|
37
|
+
//# sourceMappingURL=formats.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formats.d.ts","sourceRoot":"","sources":["../src/formats.ts"],"names":[],"mappings":"AAQA,MAAM,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAC5C,MAAM,MAAM,WAAW,GAAG,KAAK,GAAG;IAAE,EAAE,EAAE,OAAO,CAAA;CAAE,CAAC;AAClD,MAAM,MAAM,gBAAgB,GAAG,WAAW,GAAG,SAAS,CAAC;AAEvD,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,gBAAgB,CAAC;CACxB;AAED,MAAM,WAAW,OAAO;IACtB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,WAAW,CAAC;CACrB;AAED,MAAM,WAAW,eAAe;IAC9B,OAAO,EAAE,OAAO,CAAC;IACjB,OAAO,CAAC,MAAM,EAAE,WAAW,GAAG,OAAO,EAAE,CAAC;IACxC,aAAa,CAAC,MAAM,EAAE,WAAW,GAAG,GAAG,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;CAC/D;AAED,MAAM,WAAW,MAAM;IACrB,QAAQ,CAAC,YAAY,EAAE,KAAK,GAAG,OAAO,CAAC;IACvC,QAAQ,CAAC,sBAAsB,EAAE,OAAO,CAAC;IACzC,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;IAC1C,eAAe,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,gBAAgB,GAAG,MAAM,CAAC;IAC7D,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC;IAC3B,QAAQ,CACN,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,WAAW,GAAG,WAAW,EAAE,EACjC,KAAK,EAAE,KAAK,EACZ,OAAO,EAAE,eAAe,GACvB,KAAK,CAAC;IACT,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,OAAO,CAAC;CAClD;AAED,KAAK,OAAO,GAAG,OAAO,GAAG,OAAO,GAAG,MAAM,CAAC;AAyD1C,wBAAgB,WAAW,IAAI,MAAM,CAapC;AAED,MAAM,WAAW,WAAW;IAC1B,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,IAAI,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,CAAC;CACjC;AAED,wBAAgB,IAAI,CAAC,OAAO,GAAE,WAAgB,GAAG,MAAM,CAmBtD;AAOD,wBAAgB,OAAO,IAAI,MAAM,CAoFhC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { HttpResponse, passthrough } from 'msw';
|
|
2
|
+
export { api, reply, type Call, type Handle } from './api.ts';
|
|
3
|
+
export { configure, type Options } from './config.ts';
|
|
4
|
+
export { belongsTo, build, buildList, defineFactory, factoryOf, fragment, hasMany, mergePayloads, payload, sequence, sideload, type FactoryDefinition, } from './factories.ts';
|
|
5
|
+
export { activeModel, jsonApi, rest, type Format, type RestOptions, } from './formats.ts';
|
|
6
|
+
export { seed, seedList, seedRecord, sourceOf } from './store.ts';
|
|
7
|
+
export { setupApplicationTest, setupRenderingTest, setupTest, } from './setup.ts';
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,KAAK,CAAC;AAEhD,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,KAAK,IAAI,EAAE,KAAK,MAAM,EAAE,MAAM,UAAU,CAAC;AAC9D,OAAO,EAAE,SAAS,EAAE,KAAK,OAAO,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,EACL,SAAS,EACT,KAAK,EACL,SAAS,EACT,aAAa,EACb,SAAS,EACT,QAAQ,EACR,OAAO,EACP,aAAa,EACb,OAAO,EACP,QAAQ,EACR,QAAQ,EACR,KAAK,iBAAiB,GACvB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACL,WAAW,EACX,OAAO,EACP,IAAI,EACJ,KAAK,MAAM,EACX,KAAK,WAAW,GACjB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAClE,OAAO,EACL,oBAAoB,EACpB,kBAAkB,EAClB,SAAS,GACV,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare function pluralize(word: string): string;
|
|
2
|
+
export declare function singularize(word: string): string;
|
|
3
|
+
export declare function underscore(word: string): string;
|
|
4
|
+
export declare function dasherize(word: string): string;
|
|
5
|
+
export declare function camelize(word: string): string;
|
|
6
|
+
//# sourceMappingURL=inflection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inflection.d.ts","sourceRoot":"","sources":["../src/inflection.ts"],"names":[],"mappings":"AA2BA,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE9C;AAED,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEhD;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAK/C;AAED,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAE9C;AAED,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAM7C"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type HttpHandler } from 'msw';
|
|
2
|
+
export interface NearMiss {
|
|
3
|
+
expected: Record<string, unknown>;
|
|
4
|
+
received: Record<string, unknown>;
|
|
5
|
+
}
|
|
6
|
+
export declare const nearMisses: Map<string, NearMiss[]>;
|
|
7
|
+
export declare function setFailureReporter(next: (failure: Error) => void): () => void;
|
|
8
|
+
export declare function reportFailure(failure: unknown): void;
|
|
9
|
+
export declare function isBypassed(url: string): boolean;
|
|
10
|
+
export declare function finishRequest(requestId: string): void;
|
|
11
|
+
export declare function useHandler(handler: HttpHandler): void;
|
|
12
|
+
export declare function enableNetwork(): void;
|
|
13
|
+
export declare function disableNetwork(): void;
|
|
14
|
+
export declare function settleRequests(): Promise<void>;
|
|
15
|
+
//# sourceMappingURL=network.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"network.d.ts","sourceRoot":"","sources":["../src/network.ts"],"names":[],"mappings":"AAOA,OAAO,EAAmC,KAAK,WAAW,EAAE,MAAM,KAAK,CAAC;AAexE,MAAM,WAAW,QAAQ;IACvB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAClC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACnC;AAED,eAAO,MAAM,UAAU,yBAAgC,CAAC;AAKxD,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,CAAC,OAAO,EAAE,KAAK,KAAK,IAAI,GAAG,MAAM,IAAI,CAM7E;AAED,wBAAgB,aAAa,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI,CAEpD;AAmCD,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAO/C;AAuCD,wBAAgB,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAIrD;AAiCD,wBAAgB,UAAU,CAAC,OAAO,EAAE,WAAW,GAAG,IAAI,CAErD;AAED,wBAAgB,aAAa,IAAI,IAAI,CAQpC;AAED,wBAAgB,cAAc,IAAI,IAAI,CAOrC;AAID,wBAAsB,cAAc,IAAI,OAAO,CAAC,IAAI,CAAC,CAoBpD"}
|