strapi-v4.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /// <reference types="cypress" />
  2. /// <reference types="../../index.d.ts" />
  3. import { getIdFromURL } from "../../../utils";
  4. const hostname = "api.strapi-v4.refine.dev";
  5. const BASE_PATH = "/api";
  6. Cypress.Commands.add("interceptStrapiV4GETPosts", () => {
  7. return cy
  8. .intercept(
  9. {
  10. method: "GET",
  11. hostname: hostname,
  12. pathname: `${BASE_PATH}/posts`,
  13. },
  14. {
  15. fixture: "posts.json",
  16. },
  17. )
  18. .as("strapiV4GetPosts");
  19. });
  20. Cypress.Commands.add("interceptStrapiV4GETPost", () => {
  21. return cy
  22. .fixture("posts")
  23. .then((posts) => {
  24. return cy.intercept(
  25. {
  26. method: "GET",
  27. hostname: hostname,
  28. pathname: `${BASE_PATH}/posts/*`,
  29. },
  30. (req) => {
  31. const id = getIdFromURL(req.url);
  32. const post = posts.find(
  33. (post) => post.id.toString() === id.toString(),
  34. );
  35. if (!post) {
  36. req.reply(404, {});
  37. return;
  38. }
  39. req.reply({
  40. data: post,
  41. meta: {},
  42. });
  43. },
  44. );
  45. })
  46. .as("strapiV4GetPost");
  47. });
  48. Cypress.Commands.add("interceptStrapiV4POSTPost", () => {
  49. return cy.fixture("posts").then((posts) =>
  50. cy
  51. .intercept(
  52. {
  53. method: "POST",
  54. hostname: hostname,
  55. pathname: `${BASE_PATH}/posts`,
  56. },
  57. (req) => {
  58. const merged = Object.assign({}, req.body, {
  59. id: posts.length + 1,
  60. });
  61. return req.reply(merged);
  62. },
  63. )
  64. .as("strapiV4PostPost"),
  65. );
  66. });
  67. Cypress.Commands.add("interceptStrapiV4PUTPost", () => {
  68. return cy
  69. .fixture("posts")
  70. .then((posts) => {
  71. return cy.intercept(
  72. {
  73. method: "PUT",
  74. hostname: hostname,
  75. pathname: `${BASE_PATH}/posts/*`,
  76. },
  77. (req) => {
  78. const id = getIdFromURL(req.url);
  79. const post = posts.find((post) => post.id === id);
  80. if (!post) {
  81. return req.reply(404, {});
  82. }
  83. const merged = Object.assign({}, post, req.body);
  84. return req.reply(merged);
  85. },
  86. );
  87. })
  88. .as("strapiV4PutPost");
  89. });
  90. Cypress.Commands.add("interceptStrapiV4DELETEPost", () => {
  91. return cy
  92. .intercept(
  93. {
  94. method: "DELETE",
  95. hostname: hostname,
  96. pathname: `${BASE_PATH}/posts/*`,
  97. },
  98. {},
  99. )
  100. .as("strapiV4DeletePost");
  101. });
  102. Cypress.Commands.add("interceptStrapiV4GETCategories", () => {
  103. return cy
  104. .intercept(
  105. {
  106. method: "GET",
  107. hostname: hostname,
  108. pathname: `${BASE_PATH}/categories`,
  109. },
  110. { fixture: "categories.json" },
  111. )
  112. .as("strapiV4GetCategories");
  113. });
  114. Cypress.Commands.add("interceptStrapiV4GETCategory", () => {
  115. return cy
  116. .fixture("categories")
  117. .then((categories) => {
  118. return cy.intercept(
  119. {
  120. method: "GET",
  121. hostname: hostname,
  122. pathname: `${BASE_PATH}/categories/*`,
  123. },
  124. (req) => {
  125. const id = getIdFromURL(req.url);
  126. const category = categories.find(
  127. (category) => category.id.toString() === id.toString(),
  128. );
  129. if (!category) {
  130. req.reply(404, {});
  131. return;
  132. }
  133. req.reply({
  134. data: category,
  135. });
  136. },
  137. );
  138. })
  139. .as("strapiV4GetCategory");
  140. });