@akinon/pz-basket-gift-pack 2.0.4 → 2.0.5-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # @akinon/pz-basket-gift-pack
2
2
 
3
+ ## 2.0.5-rc.0
4
+
5
+ ### Patch Changes
6
+
7
+ - 823a4449: ZERO-4193: Upgrade TypeScript to version 5.2.2 across multiple packages
8
+ - 0df6bc1f: ZERO-4200: Add Jest configuration and initial tests for basket gift pack endpoints
9
+
3
10
  ## 2.0.4
4
11
 
5
12
  ## 2.0.3
package/jest.config.js ADDED
@@ -0,0 +1,7 @@
1
+ /** @type {import('ts-jest').JestConfigWithTsJest} **/
2
+ module.exports = {
3
+ testEnvironment: 'node',
4
+ transform: {
5
+ '^.+.tsx?$': ['ts-jest', {}]
6
+ }
7
+ };
package/package.json CHANGED
@@ -1,13 +1,18 @@
1
1
  {
2
2
  "name": "@akinon/pz-basket-gift-pack",
3
- "version": "2.0.4",
3
+ "version": "2.0.5-rc.0",
4
4
  "license": "MIT",
5
5
  "main": "src/index.tsx",
6
+ "scripts": {
7
+ "test": "jest",
8
+ "test:watch": "jest --watch"
9
+ },
6
10
  "peerDependencies": {
7
11
  "react": "^18.0.0 || ^19.0.0",
8
12
  "react-dom": "^18.0.0 || ^19.0.0"
9
13
  },
10
14
  "devDependencies": {
15
+ "@types/jest": "^29.5.14",
11
16
  "@types/node": "^18.7.8",
12
17
  "@types/react": "^18.0.17",
13
18
  "@types/react-dom": "^18.0.6",
@@ -0,0 +1,125 @@
1
+ import { beforeEach, describe, expect, it, jest } from '@jest/globals';
2
+
3
+ type GiftPackagePayload = { id: number; message: string };
4
+ type RemoveGiftPackagePayload = { id: number };
5
+ type RequestConfig = {
6
+ url: string;
7
+ method: string;
8
+ body: {
9
+ attributes: {
10
+ gift_note: string;
11
+ };
12
+ id: number;
13
+ is_all: boolean;
14
+ };
15
+ };
16
+ type EndpointDefinition<Payload> = {
17
+ query: (payload: Payload) => RequestConfig;
18
+ transformResponse: (response: { basket: unknown }) => unknown;
19
+ invalidatesTags: string[];
20
+ };
21
+ type EndpointMap = {
22
+ addGiftPackage: EndpointDefinition<GiftPackagePayload>;
23
+ removeGiftPackage: EndpointDefinition<RemoveGiftPackagePayload>;
24
+ };
25
+
26
+ const mockBuildClientRequestUrl = jest.fn(() => 'mocked-url');
27
+ const mockEndpointDefinitions = {} as Partial<EndpointMap>;
28
+
29
+ const mockInjectEndpoints = jest.fn(
30
+ (config: {
31
+ endpoints: (build: {
32
+ mutation: <Payload>(definition: EndpointDefinition<Payload>) => EndpointDefinition<Payload>;
33
+ }) => EndpointMap;
34
+ }) => {
35
+ const build = {
36
+ mutation: <Payload,>(definition: EndpointDefinition<Payload>) => definition
37
+ };
38
+ Object.assign(mockEndpointDefinitions, config.endpoints(build));
39
+
40
+ return {
41
+ useAddGiftPackageMutation: jest.fn(),
42
+ useRemoveGiftPackageMutation: jest.fn()
43
+ };
44
+ }
45
+ );
46
+
47
+ jest.mock('@akinon/next/utils', () => ({
48
+ buildClientRequestUrl: (...args: Parameters<typeof mockBuildClientRequestUrl>) =>
49
+ mockBuildClientRequestUrl(...args)
50
+ }));
51
+
52
+ jest.mock('@akinon/next/data/client/api', () => ({
53
+ api: {
54
+ injectEndpoints: (...args: Parameters<typeof mockInjectEndpoints>) =>
55
+ mockInjectEndpoints(...args)
56
+ }
57
+ }));
58
+
59
+ import '../endpoints';
60
+
61
+ describe('basket gift pack endpoints', () => {
62
+ beforeEach(() => {
63
+ jest.clearAllMocks();
64
+ });
65
+
66
+ it('builds addGiftPackage request payload correctly', () => {
67
+ const request = mockEndpointDefinitions.addGiftPackage?.query({
68
+ id: 42,
69
+ message: 'Happy birthday'
70
+ });
71
+
72
+ expect(mockBuildClientRequestUrl).toHaveBeenCalledWith('/baskets/basket_items', {
73
+ useTrailingSlash: false,
74
+ contentType: 'application/json'
75
+ });
76
+ expect(request).toEqual({
77
+ url: 'mocked-url',
78
+ method: 'PATCH',
79
+ body: {
80
+ attributes: {
81
+ gift_note: 'Happy birthday'
82
+ },
83
+ id: 42,
84
+ is_all: true
85
+ }
86
+ });
87
+ expect(mockEndpointDefinitions.addGiftPackage?.invalidatesTags).toEqual(['Basket']);
88
+ });
89
+
90
+ it('builds removeGiftPackage request payload correctly', () => {
91
+ const request = mockEndpointDefinitions.removeGiftPackage?.query({ id: 99 });
92
+
93
+ expect(mockBuildClientRequestUrl).toHaveBeenCalledWith('/baskets/basket_items', {
94
+ contentType: 'application/json'
95
+ });
96
+ expect(request).toEqual({
97
+ url: 'mocked-url',
98
+ method: 'PATCH',
99
+ body: {
100
+ attributes: {
101
+ gift_note: ''
102
+ },
103
+ id: 99,
104
+ is_all: true
105
+ }
106
+ });
107
+ expect(mockEndpointDefinitions.removeGiftPackage?.invalidatesTags).toEqual(['Basket']);
108
+ });
109
+
110
+ it('returns basket from addGiftPackage response transformer', () => {
111
+ const basket = { id: 7, items: [] };
112
+
113
+ expect(
114
+ mockEndpointDefinitions.addGiftPackage?.transformResponse({ basket })
115
+ ).toBe(basket);
116
+ });
117
+
118
+ it('returns basket from removeGiftPackage response transformer', () => {
119
+ const basket = { id: 8, items: [] };
120
+
121
+ expect(
122
+ mockEndpointDefinitions.removeGiftPackage?.transformResponse({ basket })
123
+ ).toBe(basket);
124
+ });
125
+ });