From 8f726b46668bd0a786989c6ba7cdff5457b5f4f9 Mon Sep 17 00:00:00 2001 From: Julian Labatut Date: Thu, 12 Jan 2023 16:01:56 +0100 Subject: [PATCH] feat(#35): add mocks for attachments --- packages/mock-server/fixtures/attachments.ts | 121 +++++++++++++++++++ packages/mock-server/fixtures/video.ts | 2 +- packages/mock-server/index.ts | 12 +- 3 files changed, 131 insertions(+), 4 deletions(-) create mode 100644 packages/mock-server/fixtures/attachments.ts diff --git a/packages/mock-server/fixtures/attachments.ts b/packages/mock-server/fixtures/attachments.ts new file mode 100644 index 00000000..e3091e2e --- /dev/null +++ b/packages/mock-server/fixtures/attachments.ts @@ -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) => { + 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): 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): void { + server.createList(this.name(), 20); + } + + model(): ModelDefinition { + 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, +}; diff --git a/packages/mock-server/fixtures/video.ts b/packages/mock-server/fixtures/video.ts index eb1e7795..6dc26d2b 100644 --- a/packages/mock-server/fixtures/video.ts +++ b/packages/mock-server/fixtures/video.ts @@ -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", diff --git a/packages/mock-server/index.ts b/packages/mock-server/index.ts index c3fc38e3..e115fff5 100644 --- a/packages/mock-server/index.ts +++ b/packages/mock-server/index.ts @@ -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 @@ -13,6 +14,7 @@ const mocks: Mock[] = [ new VideoMock(), new SearchMock(), new CourseMock(), + new AttachmentMock(), ]; function initModels(): { [key: string]: ModelDefinition } { @@ -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, + ReturnType +>;