Skip to content

Commit 8f726b4

Browse files
committed
feat(#35): add mocks for attachments
1 parent d0da952 commit 8f726b4

File tree

3 files changed

+131
-4
lines changed

3 files changed

+131
-4
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import { AppRegistry } from "..";
2+
import { Mock } from "./generic";
3+
import { BaseUsers } from "./user";
4+
import { readableVideo } from "./video";
5+
import { faker } from "@faker-js/faker";
6+
import { Factory, Model, Server } from "miragejs";
7+
import { FactoryDefinition, ModelDefinition } from "miragejs/-types";
8+
9+
export interface Attachment {
10+
id: string;
11+
title: string;
12+
url: string;
13+
description?: string;
14+
userId: string;
15+
status: AttachmentStatus;
16+
type: AttachmentType;
17+
videos: string[];
18+
modules: string[];
19+
}
20+
21+
export class AttachmentMock implements Mock {
22+
name(): string {
23+
return "attachment";
24+
}
25+
26+
/**
27+
* Converts page and pageSize to start and end, to use with `slice` method
28+
*/
29+
getPagination = (queryParams: Record<string, string>) => {
30+
const page = parseInt(queryParams.page);
31+
const pageSize = parseInt(queryParams.pageSize);
32+
const start = (page - 1) * pageSize;
33+
const end = start + pageSize;
34+
return { start, end };
35+
};
36+
37+
routes(server: Server<AppRegistry>): void {
38+
server.get("attachments/", (schema, { queryParams }) => {
39+
const { start, end } = this.getPagination(queryParams);
40+
const models = schema.all("attachment");
41+
return {
42+
items: models.slice(start, end).models,
43+
totalCount: models.length,
44+
};
45+
});
46+
47+
server.get("attachments/video/:videoId", (schema) => {
48+
const { models } = schema.all("attachment");
49+
return {
50+
items: models,
51+
totalCount: models.length,
52+
};
53+
});
54+
55+
server.get("attachments/:id", (schema, { params }) => {
56+
return schema.find("attachment", params.id);
57+
});
58+
59+
server.post("attachments", (schema, request) => {
60+
return schema.create("attachment", JSON.parse(request.requestBody));
61+
});
62+
}
63+
64+
factory(): FactoryDefinition<{}> {
65+
return Factory.extend({
66+
id() {
67+
return faker.datatype.uuid();
68+
},
69+
title() {
70+
return faker.internet.domainWord();
71+
},
72+
description() {
73+
return faker.datatype.string();
74+
},
75+
url() {
76+
return faker.internet.url();
77+
},
78+
userId() {
79+
return BaseUsers[0].id;
80+
},
81+
status() {
82+
return AttachmentStatus.Completed;
83+
},
84+
videos() {
85+
return [];
86+
},
87+
modules() {
88+
return [];
89+
},
90+
});
91+
}
92+
93+
seeds(server: Server<AppRegistry>): void {
94+
server.createList(this.name(), 20);
95+
}
96+
97+
model(): ModelDefinition<Attachment> {
98+
return Model.extend({});
99+
}
100+
}
101+
102+
export enum AttachmentStatus {
103+
InProgress = "IN_PROGRESS",
104+
Completed = "COMPLETED",
105+
}
106+
107+
export enum AttachmentType {
108+
External = "EXTERNAL",
109+
Internal = "INTERNAL",
110+
}
111+
112+
export const mockAttachment: Attachment = {
113+
id: "139c2ab0-4e66-48ff-9149-d66782432bfa",
114+
title: "Google",
115+
url: "https://google.com",
116+
videos: [readableVideo.id],
117+
modules: [],
118+
type: AttachmentType.External,
119+
status: AttachmentStatus.Completed,
120+
userId: BaseUsers[0].id,
121+
};

packages/mock-server/fixtures/video.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export class VideoMock implements Mock {
124124
}
125125
}
126126

127-
const readableVideo: Video = {
127+
export const readableVideo: Video = {
128128
id: "50d4ec43-4e66-48ff-9149-d6678243815c",
129129
slug: "angular-in-100-seconds",
130130
title: "Angular in 100 seconds",

packages/mock-server/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { createServer } from "miragejs";
2-
import { FactoryDefinition, ModelDefinition } from "miragejs/-types";
1+
import { AttachmentMock } from "./fixtures/attachments";
32
import { CourseMock } from "./fixtures/course";
43
import { Mock } from "./fixtures/generic";
54
import { SearchMock } from "./fixtures/search";
65
import { UserMock } from "./fixtures/user";
76
import { VideoMock } from "./fixtures/video";
7+
import { createServer, Registry } from "miragejs";
8+
import { FactoryDefinition, ModelDefinition } from "miragejs/-types";
89

910
// Add future mock implementation here
1011
// The server will autoconfigure itself thanks to the following array
@@ -13,6 +14,7 @@ const mocks: Mock[] = [
1314
new VideoMock(),
1415
new SearchMock(),
1516
new CourseMock(),
17+
new AttachmentMock(),
1618
];
1719

1820
function initModels(): { [key: string]: ModelDefinition } {
@@ -40,10 +42,14 @@ export function initMockServer() {
4042
mocks.forEach((mock) => mock.seeds(server));
4143
},
4244
routes() {
43-
this.urlPrefix = 'http://localhost:4000';
45+
this.urlPrefix = "http://localhost:4000";
4446
mocks.forEach((mock) => mock.routes(this));
4547
},
4648
});
4749
}
4850

4951
export { BaseUsers } from "./fixtures/user";
52+
export type AppRegistry = Registry<
53+
ReturnType<typeof initModels>,
54+
ReturnType<typeof initFactories>
55+
>;

0 commit comments

Comments
 (0)