Skip to content

Commit 6ffb427

Browse files
ReidyTLinaYahya
andauthored
deps(msw): migrates to v.2 (#389)
* feat: mocks upload app settings files Co-authored-by: LinaYahya <[email protected]>
1 parent e2681a7 commit 6ffb427

9 files changed

+506
-594
lines changed

README.md

+47-12
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,16 @@ if (process.env.REACT_APP_ENABLE_MOCK_API === 'true') {
101101
```js
102102
import { buildDatabase } from '@graasp/apps-query-client';
103103

104-
Cypress.Commands.add(
105-
'setUpApi',
106-
({ currentMember = CURRENT_MEMBER, database = {}, appContext } = {}) => {
107-
// mock api and database
108-
Cypress.on('window:before:load', (win) => {
109-
win.database = buildDatabase({
110-
members: Object.values(MEMBERS),
111-
...database,
112-
});
113-
win.appContext = appContext;
104+
Cypress.Commands.add('setUpApi', ({ currentMember = CURRENT_MEMBER, database = {}, appContext } = {}) => {
105+
// mock api and database
106+
Cypress.on('window:before:load', (win) => {
107+
win.database = buildDatabase({
108+
members: Object.values(MEMBERS),
109+
...database,
114110
});
115-
},
116-
);
111+
win.appContext = appContext;
112+
});
113+
});
117114
```
118115

119116
3. Then in all your tests you will need to set up the database and context. The default values are configured so you can easily mount an empty and operational database.
@@ -131,3 +128,41 @@ cy.setUpApi({
131128
},
132129
});
133130
```
131+
132+
### Mock Uploaded files
133+
134+
If you need to have files in the mocked server, you can use the `uploadedFiles` array of the `setupApi`. The following are steps to follow if you want to upload and retrieve a file for an app.
135+
136+
1. Create a new `AppSetting` and add it in the `appSettings` array of the `setupApi` (in the Cypress test of the frontend).
137+
138+
```ts
139+
// MOCK_FILE_APP_SETTING
140+
{
141+
id: mockFileSettingId,
142+
name: 'file', // should be named `file`! Do not change it!
143+
data: {
144+
path: `apps/app-setting/${item.id}/${mockFileSettingId}`, // This path should be mocked in the MSW! If you want to use another path, you just have to mock it.
145+
},
146+
item,
147+
creator,
148+
createdAt,
149+
updatedAt,
150+
};
151+
```
152+
153+
2. Load a file and transform it into a `File` type. With Cypress, have a look to `cy.fixtures` (put the `setupApi` in the `then` callback).
154+
3. Add the loaded file in the array.
155+
156+
```ts
157+
uploadedFiles: [
158+
{
159+
id: MOCK_FILE_APP_SETTING.id,
160+
file,
161+
},
162+
],
163+
```
164+
165+
4. Use the mocked route `GET /app-items/app-settings/:appSettingId/download` (or your mocked route) to retrieve the path of the file.
166+
5. Use the result of the previous request to download the file. In this example, it's the route `GET /download-app-setting-url/:appSettingId`.
167+
168+
Another solution could be to upload your file from the Cypress test, by using the route `/app-items/app-settings-/upload?id=:itemId` for example.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"dexie": "4.0.8",
4747
"http-status-codes": "2.3.0",
4848
"miragejs": "0.1.48",
49-
"msw": "1.3.3",
49+
"msw": "2.3.5",
5050
"uuid": "10.0.0"
5151
},
5252
"devDependencies": {

src/mockServer/fixtures.ts

+2
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ export const buildDatabase = ({
5858
appSettings = [],
5959
members = [MOCK_SERVER_MEMBER],
6060
items = [MOCK_SERVER_ITEM],
61+
uploadedFiles = [],
6162
}: Partial<Database> = {}): Database => ({
6263
appData,
6364
appActions,
6465
appSettings,
6566
members,
6667
items,
68+
uploadedFiles,
6769
});

src/mockServer/msw/dexie-db.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { AppAction, AppData, AppSetting, DiscriminatedItem, Member } from '@graa
22

33
import Dexie from 'dexie';
44

5-
import { Database, LocalContext } from '../../types';
5+
import { Database, LocalContext, MockUploadedFile } from '../../types';
66

77
type OptionalIndexed<T extends { id: string }, P extends keyof T = 'id'> = {
88
[Key in keyof T as Key extends P ? Key : never]?: T[Key];
@@ -27,6 +27,8 @@ export class AppMocks extends Dexie {
2727

2828
appAction!: Dexie.Table<AppAction, string>;
2929

30+
uploadedFiles!: Dexie.Table<MockUploadedFile, string>;
31+
3032
constructor(name?: string) {
3133
super(name ?? 'graasp-app-mocks');
3234

@@ -42,6 +44,10 @@ export class AppMocks extends Dexie {
4244
appSetting: 'id, item.id, name',
4345
appAction: 'id, memberId',
4446
});
47+
48+
this.version(2).stores({
49+
uploadedFiles: 'id',
50+
});
4551
}
4652

4753
seed(data: Partial<Database> & { appContext?: LocalContext }): void {
@@ -64,6 +70,9 @@ export class AppMocks extends Dexie {
6470
if (data.appActions?.length) {
6571
this.appAction.bulkAdd(data.appActions);
6672
}
73+
if (data.uploadedFiles?.length) {
74+
this.uploadedFiles.bulkAdd(data.uploadedFiles);
75+
}
6776
}
6877

6978
resetDB(data?: Partial<Database> & { appContext?: LocalContext }): void {

0 commit comments

Comments
 (0)