supabase.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /// <reference types="cypress" />
  2. /// <reference types="../../index.d.ts" />
  3. import { ICategory, IPost } from "../../types";
  4. const HOSTNAME = "iwdfzvfqbtokqetmbmbp.supabase.co";
  5. const BASE_PATH = "/rest/v1";
  6. const getSupabaseIdFromQuery = (query?: Record<string, string | number>) => {
  7. // supabase uses id in query like this {id: 'eq.1'}
  8. return (query?.id as string)?.split(".")?.[1];
  9. };
  10. Cypress.Commands.add("interceptSupabaseGETPosts", () => {
  11. // read posts and categories from fixtures
  12. let posts: (IPost & {
  13. categories: ICategory;
  14. })[] = [];
  15. let categories: ICategory[] = [];
  16. cy.fixture("categories").then((categoriesFixture) => {
  17. categories = categoriesFixture;
  18. });
  19. // transform fixtures to match supabase response
  20. cy.fixture("posts").then((rawPosts) => {
  21. posts = rawPosts.map((post) => {
  22. // in supabase, the category is not object, but in fixture it is
  23. // because of that, we need to convert it to categoryId
  24. return Object.assign({}, post, {
  25. categoryId: post.category.id,
  26. categories: categories.find(
  27. (category) => category.id === post.category.id,
  28. ),
  29. });
  30. });
  31. });
  32. return cy
  33. .intercept(
  34. {
  35. method: "GET",
  36. hostname: HOSTNAME,
  37. pathname: `${BASE_PATH}/posts`,
  38. },
  39. (req) => {
  40. const id = getSupabaseIdFromQuery(req.query);
  41. if (id) {
  42. const post = posts.find(
  43. (post) => post.id.toString() === id.toString(),
  44. );
  45. if (!post) {
  46. return req.reply(404, []);
  47. }
  48. return req.reply([post]);
  49. }
  50. return req.reply(posts);
  51. },
  52. )
  53. .as("supabaseGetPosts");
  54. });
  55. Cypress.Commands.add("interceptSupabasePOSTPost", () => {
  56. return cy.fixture("posts").then((posts) =>
  57. cy
  58. .intercept(
  59. {
  60. method: "POST",
  61. hostname: HOSTNAME,
  62. pathname: `${BASE_PATH}/posts`,
  63. },
  64. (req) => {
  65. const merged = Object.assign({}, req.body, {
  66. id: posts.length + 1,
  67. });
  68. return req.reply(merged);
  69. },
  70. )
  71. .as("supabasePostPost"),
  72. );
  73. });
  74. Cypress.Commands.add("interceptSupabasePATCHPost", () => {
  75. return cy
  76. .fixture("posts")
  77. .then((posts) => {
  78. return cy.intercept(
  79. {
  80. method: "PATCH",
  81. hostname: HOSTNAME,
  82. pathname: `${BASE_PATH}/posts`,
  83. },
  84. (req) => {
  85. const id = getSupabaseIdFromQuery(req.query);
  86. const post = posts.find(
  87. (post) => post.id.toString() === id.toString(),
  88. );
  89. if (!post) {
  90. return req.reply(404, {});
  91. }
  92. const merged = Object.assign({}, post, req.body);
  93. return req.reply(merged);
  94. },
  95. );
  96. })
  97. .as("supabasePatchPost");
  98. });
  99. Cypress.Commands.add("interceptSupabaseDELETEPost", () => {
  100. return cy
  101. .intercept(
  102. {
  103. method: "DELETE",
  104. hostname: HOSTNAME,
  105. pathname: `${BASE_PATH}/posts`,
  106. },
  107. {},
  108. )
  109. .as("supabaseDeletePost");
  110. });
  111. Cypress.Commands.add("interceptSupabaseGETCategories", () => {
  112. return cy
  113. .intercept(
  114. {
  115. method: "GET",
  116. hostname: HOSTNAME,
  117. pathname: `${BASE_PATH}/categories*`,
  118. },
  119. { fixture: "categories.json" },
  120. )
  121. .as("supabaseGetCategories");
  122. });