123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- /// <reference types="cypress" />
- /// <reference types="../../index.d.ts" />
- import { getIdFromURL } from "../../../utils";
- const hostname = "api.fake-rest.refine.dev";
- Cypress.Commands.add("interceptGETPosts", () => {
- return cy
- .intercept(
- {
- method: "GET",
- hostname: hostname,
- pathname: "/posts",
- },
- {
- fixture: "posts.json",
- },
- )
- .as("getPosts");
- });
- Cypress.Commands.add("interceptGETPost", () => {
- return cy
- .fixture("posts")
- .then((posts) => {
- return cy.intercept(
- {
- method: "GET",
- hostname: hostname,
- pathname: "/posts/*",
- },
- (req) => {
- const id = getIdFromURL(req.url);
- const post = posts.find((post) => post.id === id);
- if (!post) {
- req.reply(404, {});
- return;
- }
- req.reply(post);
- },
- );
- })
- .as("getPost");
- });
- Cypress.Commands.add("interceptPOSTPost", () => {
- return cy.fixture("posts").then((posts) =>
- cy
- .intercept(
- {
- method: "POST",
- hostname: hostname,
- pathname: "/posts",
- },
- (req) => {
- const merged = Object.assign({}, req.body, {
- id: posts.length + 1,
- });
- return req.reply(merged);
- },
- )
- .as("postPost"),
- );
- });
- Cypress.Commands.add("interceptPATCHPost", () => {
- return cy
- .fixture("posts")
- .then((posts) => {
- return cy.intercept(
- {
- method: "PATCH",
- hostname: hostname,
- pathname: "/posts/*",
- },
- (req) => {
- const id = getIdFromURL(req.url);
- const post = posts.find((post) => post.id === id);
- if (!post) {
- return req.reply(404, {});
- }
- const merged = Object.assign({}, post, req.body);
- return req.reply(merged);
- },
- );
- })
- .as("patchPost");
- });
- Cypress.Commands.add("interceptDELETEPost", () => {
- return cy
- .intercept(
- {
- method: "DELETE",
- hostname: hostname,
- pathname: "/posts/*",
- },
- {},
- )
- .as("deletePost");
- });
- Cypress.Commands.add("interceptGETCategories", () => {
- return cy
- .intercept(
- {
- method: "GET",
- hostname: hostname,
- pathname: "/categories",
- },
- { fixture: "categories.json" },
- )
- .as("getCategories");
- });
- Cypress.Commands.add("interceptGETCategory", () => {
- return cy
- .fixture("categories")
- .then((categories) => {
- return cy.intercept(
- {
- method: "GET",
- hostname: hostname,
- pathname: "/categories/*",
- },
- (req) => {
- const id = getIdFromURL(req.url);
- const category = categories.find(
- (category) => category.id.toString() === id.toString(),
- );
- if (!category) {
- req.reply(404, {});
- return;
- }
- req.reply(category);
- },
- );
- })
- .as("getCategory");
- });
- Cypress.Commands.add("interceptGETBlogPosts", () => {
- return cy
- .intercept(
- {
- method: "GET",
- hostname: hostname,
- pathname: "/blog_posts",
- },
- {
- fixture: "blog-posts.json",
- },
- )
- .as("getBlogPosts");
- });
- Cypress.Commands.add("interceptGETBlogPost", () => {
- return cy
- .fixture("blog-posts")
- .then((posts) => {
- return cy.intercept(
- {
- method: "GET",
- hostname: hostname,
- pathname: "/blog_posts/*",
- },
- (req) => {
- const id = getIdFromURL(req.url);
- const post = posts.find((post) => post.id === id);
- if (!post) {
- req.reply(404, {});
- return;
- }
- req.reply(post);
- },
- );
- })
- .as("getBlogPost");
- });
- Cypress.Commands.add("interceptPOSTBlogPost", () => {
- return cy.fixture("blog-posts").then((posts) =>
- cy
- .intercept(
- {
- method: "POST",
- hostname: hostname,
- pathname: "/blog_posts",
- },
- (req) => {
- const merged = Object.assign({}, req.body, {
- id: posts.length + 1,
- });
- return req.reply(merged);
- },
- )
- .as("postBlogPost"),
- );
- });
- Cypress.Commands.add("interceptPATCHBlogPost", () => {
- return cy
- .fixture("blog-posts")
- .then((posts) => {
- return cy.intercept(
- {
- method: "PATCH",
- hostname: hostname,
- pathname: "/blog_posts/*",
- },
- (req) => {
- const id = getIdFromURL(req.url);
- const post = posts.find((post) => post.id === id);
- if (!post) {
- return req.reply(404, {});
- }
- const merged = Object.assign({}, post, req.body);
- return req.reply(merged);
- },
- );
- })
- .as("patchBlogPost");
- });
- Cypress.Commands.add("interceptDELETEBlogPost", () => {
- return cy
- .intercept(
- {
- method: "DELETE",
- hostname: hostname,
- pathname: "/blog_posts/*",
- },
- {},
- )
- .as("deleteBlogPost");
- });
|