Skip to content

Commit

Permalink
feat(#35): add mocks for attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
jlabatut committed Jan 12, 2023
1 parent d0da952 commit 8f726b4
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 4 deletions.
121 changes: 121 additions & 0 deletions packages/mock-server/fixtures/attachments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { AppRegistry } from "..";
import { Mock } from "./generic";
import { BaseUsers } from "./user";
import { readableVideo } from "./video";
import { faker } from "@faker-js/faker";
import { Factory, Model, Server } from "miragejs";
import { FactoryDefinition, ModelDefinition } from "miragejs/-types";

export interface Attachment {
id: string;
title: string;
url: string;
description?: string;
userId: string;
status: AttachmentStatus;
type: AttachmentType;
videos: string[];
modules: string[];
}

export class AttachmentMock implements Mock {
name(): string {
return "attachment";
}

/**
* Converts page and pageSize to start and end, to use with `slice` method
*/
getPagination = (queryParams: Record<string, string>) => {
const page = parseInt(queryParams.page);
const pageSize = parseInt(queryParams.pageSize);
const start = (page - 1) * pageSize;
const end = start + pageSize;
return { start, end };
};

routes(server: Server<AppRegistry>): void {
server.get("attachments/", (schema, { queryParams }) => {
const { start, end } = this.getPagination(queryParams);
const models = schema.all("attachment");
return {
items: models.slice(start, end).models,
totalCount: models.length,
};
});

server.get("attachments/video/:videoId", (schema) => {
const { models } = schema.all("attachment");
return {
items: models,
totalCount: models.length,
};
});

server.get("attachments/:id", (schema, { params }) => {
return schema.find("attachment", params.id);
});

server.post("attachments", (schema, request) => {
return schema.create("attachment", JSON.parse(request.requestBody));
});
}

factory(): FactoryDefinition<{}> {
return Factory.extend({
id() {
return faker.datatype.uuid();
},
title() {
return faker.internet.domainWord();
},
description() {
return faker.datatype.string();
},
url() {
return faker.internet.url();
},
userId() {
return BaseUsers[0].id;
},
status() {
return AttachmentStatus.Completed;
},
videos() {
return [];
},
modules() {
return [];
},
});
}

seeds(server: Server<AppRegistry>): void {
server.createList(this.name(), 20);
}

model(): ModelDefinition<Attachment> {
return Model.extend({});
}
}

export enum AttachmentStatus {
InProgress = "IN_PROGRESS",
Completed = "COMPLETED",
}

export enum AttachmentType {
External = "EXTERNAL",
Internal = "INTERNAL",
}

export const mockAttachment: Attachment = {
id: "139c2ab0-4e66-48ff-9149-d66782432bfa",
title: "Google",
url: "https://google.com",
videos: [readableVideo.id],
modules: [],
type: AttachmentType.External,
status: AttachmentStatus.Completed,
userId: BaseUsers[0].id,
};
2 changes: 1 addition & 1 deletion packages/mock-server/fixtures/video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class VideoMock implements Mock {
}
}

const readableVideo: Video = {
export const readableVideo: Video = {
id: "50d4ec43-4e66-48ff-9149-d6678243815c",
slug: "angular-in-100-seconds",
title: "Angular in 100 seconds",
Expand Down
12 changes: 9 additions & 3 deletions packages/mock-server/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { createServer } from "miragejs";
import { FactoryDefinition, ModelDefinition } from "miragejs/-types";
import { AttachmentMock } from "./fixtures/attachments";
import { CourseMock } from "./fixtures/course";
import { Mock } from "./fixtures/generic";
import { SearchMock } from "./fixtures/search";
import { UserMock } from "./fixtures/user";
import { VideoMock } from "./fixtures/video";
import { createServer, Registry } from "miragejs";
import { FactoryDefinition, ModelDefinition } from "miragejs/-types";

// Add future mock implementation here
// The server will autoconfigure itself thanks to the following array
Expand All @@ -13,6 +14,7 @@ const mocks: Mock[] = [
new VideoMock(),
new SearchMock(),
new CourseMock(),
new AttachmentMock(),
];

function initModels(): { [key: string]: ModelDefinition } {
Expand Down Expand Up @@ -40,10 +42,14 @@ export function initMockServer() {
mocks.forEach((mock) => mock.seeds(server));
},
routes() {
this.urlPrefix = 'http://localhost:4000';
this.urlPrefix = "http://localhost:4000";
mocks.forEach((mock) => mock.routes(this));
},
});
}

export { BaseUsers } from "./fixtures/user";
export type AppRegistry = Registry<
ReturnType<typeof initModels>,
ReturnType<typeof initFactories>
>;

0 comments on commit 8f726b4

Please sign in to comment.