I will be showing you an awesome tool, MirageJS, that will allow you to spin up a mock API in minutes. Its more than just returning dummy JSON, Mirage allows to build relationships between your [mock] models.
from "miragejs"; export function makeServer({ environment = "test" }) { return createServer({ environment, models: { post: Model, }, routes() { / / routes here ... this.get("/api/posts", function (schema, request) { return { posts: [ { id: "1", title: "Post title 1", content: "Maecenas ut molestie ante. Ut a nisi nec dolor lacinia .. . ", }, { id: "2", title: "Post title 2", content: "Nam eget leo eget turpis mattis dignissim eget elementum ipsum ... ", }, ], }; }); this.passthrough(); }, }); }
environment = "test" }) { return createServer({ .. . routes() { / / routes here ... this.get("/api/posts", function (schema, request) { return { posts: [ { id: "1", title: "Post title 1", content: "Maecenas ut molestie ante. Ut a nisi nec dolor lacinia .. . ", }, { id: "2", title: "Post title 2", content: "Nam eget leo eget turpis mattis dignissim eget elementum ipsum ... ", }, ], }; }); this.passthrough(); }, }); }
environment = "test" }) { return createServer({ .. . routes() { / / routes here ... this.get("/api/posts", function (schema, request) { return { posts: [ { id: "1", title: "Post title 1", content: "Maecenas ut molestie ante. Ut a nisi nec dolor lacinia .. . ", }, { id: "2", title: "Post title 2", content: "Nam eget leo eget turpis mattis dignissim eget elementum ipsum ... ", }, ], }; }); this.passthrough(); }, }); }
{ // Schema:find let post = schema.posts.find(request.params.id); // Schema:findBy let post = schema.posts.findBy({ title: "First Post" }); let post = schema.posts.findBy({ authorId: 1, type: "video" }); // Schema:first let post = schema.posts.first(); return post; }); / / Database this.get("/api/posts/:id", function (schema, request) { // Database:find let post = schema.posts.find(request.params.id); // Database:findBy let post = schema.posts.findBy({ title: "First Post" }); let post = schema.posts.findBy({ authorId: 1, type: "video" }); return post; });
{ server = makeServer({ environment: "test" }); }); afterEach(() => { server.shutdown(); }); it("shows all the posts", function () { server.createList("post", 10); cy.visit("/"); cy.get("li.post").should("have.length", 10); }); it("shows the post's title on the detail route", function () { let post = this.server.create("post", { title: "Some Title", content: "Some content", }); cy.visit(`/post/${post.id}`); cy.get("h1").should("contain", "Some Title"); });