tax-category.e2e-spec.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import { createTestEnvironment } from '@vendure/testing';
  2. import gql from 'graphql-tag';
  3. import path from 'path';
  4. import { afterAll, beforeAll, describe, expect, it } from 'vitest';
  5. import { initialData } from '../../../e2e-common/e2e-initial-data';
  6. import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
  7. import { DeletionResult } from './graphql/generated-e2e-admin-types';
  8. import * as Codegen from './graphql/generated-e2e-admin-types';
  9. import { sortById } from './utils/test-order-utils';
  10. describe('TaxCategory resolver', () => {
  11. const { server, adminClient, shopClient } = createTestEnvironment(testConfig());
  12. beforeAll(async () => {
  13. await server.init({
  14. initialData,
  15. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  16. customerCount: 2,
  17. });
  18. await adminClient.asSuperAdmin();
  19. }, TEST_SETUP_TIMEOUT_MS);
  20. afterAll(async () => {
  21. await server.destroy();
  22. });
  23. it('taxCategories', async () => {
  24. const { taxCategories } = await adminClient.query<Codegen.GetTaxCategoryListQuery>(
  25. GET_TAX_CATEGORY_LIST,
  26. );
  27. expect(taxCategories.items.sort(sortById)).toEqual([
  28. { id: 'T_1', name: 'Standard Tax', isDefault: false },
  29. { id: 'T_2', name: 'Reduced Tax', isDefault: false },
  30. { id: 'T_3', name: 'Zero Tax', isDefault: false },
  31. ]);
  32. });
  33. it('taxCategory', async () => {
  34. const { taxCategory } = await adminClient.query<
  35. Codegen.GetTaxCategoryQuery,
  36. Codegen.GetTaxCategoryQueryVariables
  37. >(GET_TAX_CATEGORY, {
  38. id: 'T_2',
  39. });
  40. expect(taxCategory).toEqual({
  41. id: 'T_2',
  42. name: 'Reduced Tax',
  43. isDefault: false,
  44. });
  45. });
  46. it('createTaxCategory', async () => {
  47. const { createTaxCategory } = await adminClient.query<
  48. Codegen.CreateTaxCategoryMutation,
  49. Codegen.CreateTaxCategoryMutationVariables
  50. >(CREATE_TAX_CATEGORY, {
  51. input: {
  52. name: 'New Category',
  53. },
  54. });
  55. expect(createTaxCategory).toEqual({
  56. id: 'T_4',
  57. name: 'New Category',
  58. isDefault: false,
  59. });
  60. });
  61. it('updateCategory', async () => {
  62. const { updateTaxCategory } = await adminClient.query<
  63. Codegen.UpdateTaxCategoryMutation,
  64. Codegen.UpdateTaxCategoryMutationVariables
  65. >(UPDATE_TAX_CATEGORY, {
  66. input: {
  67. id: 'T_4',
  68. name: 'New Category Updated',
  69. },
  70. });
  71. expect(updateTaxCategory).toEqual({
  72. id: 'T_4',
  73. name: 'New Category Updated',
  74. isDefault: false,
  75. });
  76. });
  77. it('set default', async () => {
  78. const { updateTaxCategory } = await adminClient.query<
  79. Codegen.UpdateTaxCategoryMutation,
  80. Codegen.UpdateTaxCategoryMutationVariables
  81. >(UPDATE_TAX_CATEGORY, {
  82. input: {
  83. id: 'T_2',
  84. isDefault: true,
  85. },
  86. });
  87. expect(updateTaxCategory).toEqual({
  88. id: 'T_2',
  89. name: 'Reduced Tax',
  90. isDefault: true,
  91. });
  92. const { taxCategories } = await adminClient.query<Codegen.GetTaxCategoryListQuery>(
  93. GET_TAX_CATEGORY_LIST,
  94. );
  95. expect(taxCategories.items.sort(sortById)).toEqual([
  96. { id: 'T_1', name: 'Standard Tax', isDefault: false },
  97. { id: 'T_2', name: 'Reduced Tax', isDefault: true },
  98. { id: 'T_3', name: 'Zero Tax', isDefault: false },
  99. { id: 'T_4', name: 'New Category Updated', isDefault: false },
  100. ]);
  101. });
  102. it('set a different default', async () => {
  103. const { updateTaxCategory } = await adminClient.query<
  104. Codegen.UpdateTaxCategoryMutation,
  105. Codegen.UpdateTaxCategoryMutationVariables
  106. >(UPDATE_TAX_CATEGORY, {
  107. input: {
  108. id: 'T_1',
  109. isDefault: true,
  110. },
  111. });
  112. expect(updateTaxCategory).toEqual({
  113. id: 'T_1',
  114. name: 'Standard Tax',
  115. isDefault: true,
  116. });
  117. const { taxCategories } = await adminClient.query<Codegen.GetTaxCategoryListQuery>(
  118. GET_TAX_CATEGORY_LIST,
  119. );
  120. expect(taxCategories.items.sort(sortById)).toEqual([
  121. { id: 'T_1', name: 'Standard Tax', isDefault: true },
  122. { id: 'T_2', name: 'Reduced Tax', isDefault: false },
  123. { id: 'T_3', name: 'Zero Tax', isDefault: false },
  124. { id: 'T_4', name: 'New Category Updated', isDefault: false },
  125. ]);
  126. });
  127. describe('deletion', () => {
  128. it('cannot delete if used by a TaxRate', async () => {
  129. const { deleteTaxCategory } = await adminClient.query<
  130. Codegen.DeleteTaxCategoryMutation,
  131. Codegen.DeleteTaxCategoryMutationVariables
  132. >(DELETE_TAX_CATEGORY, {
  133. id: 'T_2',
  134. });
  135. expect(deleteTaxCategory.result).toBe(DeletionResult.NOT_DELETED);
  136. expect(deleteTaxCategory.message).toBe(
  137. 'Cannot remove TaxCategory "Reduced Tax" as it is referenced by 5 TaxRates',
  138. );
  139. });
  140. it('can delete if not used by TaxRate', async () => {
  141. const { deleteTaxCategory } = await adminClient.query<
  142. Codegen.DeleteTaxCategoryMutation,
  143. Codegen.DeleteTaxCategoryMutationVariables
  144. >(DELETE_TAX_CATEGORY, {
  145. id: 'T_4',
  146. });
  147. expect(deleteTaxCategory.result).toBe(DeletionResult.DELETED);
  148. expect(deleteTaxCategory.message).toBeNull();
  149. const { taxCategory } = await adminClient.query<
  150. Codegen.GetTaxCategoryQuery,
  151. Codegen.GetTaxCategoryQueryVariables
  152. >(GET_TAX_CATEGORY, {
  153. id: 'T_4',
  154. });
  155. expect(taxCategory).toBeNull();
  156. });
  157. });
  158. });
  159. const GET_TAX_CATEGORY_LIST = gql`
  160. query GetTaxCategoryList {
  161. taxCategories {
  162. items {
  163. id
  164. name
  165. isDefault
  166. }
  167. }
  168. }
  169. `;
  170. const GET_TAX_CATEGORY = gql`
  171. query GetTaxCategory($id: ID!) {
  172. taxCategory(id: $id) {
  173. id
  174. name
  175. isDefault
  176. }
  177. }
  178. `;
  179. const CREATE_TAX_CATEGORY = gql`
  180. mutation CreateTaxCategory($input: CreateTaxCategoryInput!) {
  181. createTaxCategory(input: $input) {
  182. id
  183. name
  184. isDefault
  185. }
  186. }
  187. `;
  188. const UPDATE_TAX_CATEGORY = gql`
  189. mutation UpdateTaxCategory($input: UpdateTaxCategoryInput!) {
  190. updateTaxCategory(input: $input) {
  191. id
  192. name
  193. isDefault
  194. }
  195. }
  196. `;
  197. const DELETE_TAX_CATEGORY = gql`
  198. mutation DeleteTaxCategory($id: ID!) {
  199. deleteTaxCategory(id: $id) {
  200. result
  201. message
  202. }
  203. }
  204. `;