@ripwords/myinvois-client 0.0.4 → 0.0.6

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.
@@ -1,10 +1,9 @@
1
1
  import { describe, it, expect, vi, beforeEach } from 'vitest'
2
2
  import { MyInvoisClient } from '../src/utils/MyInvoisClient'
3
- import { ofetch } from 'ofetch'
4
3
 
5
- vi.mock('ofetch', () => ({
6
- ofetch: vi.fn(),
7
- }))
4
+ // Mock global fetch
5
+ const mockFetch = vi.fn()
6
+ vi.stubGlobal('fetch', mockFetch)
8
7
 
9
8
  vi.useFakeTimers()
10
9
 
@@ -24,7 +23,7 @@ describe('MyInvoisClient', () => {
24
23
  'sandbox',
25
24
  )
26
25
  expect((sandboxClient as any).baseUrl).toBe(
27
- 'https://preprod-mytax.hasil.gov.my',
26
+ 'https://preprod-api.myinvois.hasil.gov.my',
28
27
  )
29
28
  })
30
29
 
@@ -34,7 +33,9 @@ describe('MyInvoisClient', () => {
34
33
  process.env.CLIENT_SECRET!,
35
34
  'production',
36
35
  )
37
- expect((prodClient as any).baseUrl).toBe('https://mytax.hasil.gov.my')
36
+ expect((prodClient as any).baseUrl).toBe(
37
+ 'https://api.myinvois.hasil.gov.my',
38
+ )
38
39
  })
39
40
  })
40
41
 
@@ -45,12 +46,15 @@ describe('MyInvoisClient', () => {
45
46
  expires_in: 3600,
46
47
  }
47
48
 
48
- vi.mocked(ofetch).mockResolvedValueOnce(mockToken)
49
+ mockFetch.mockResolvedValueOnce({
50
+ ok: true,
51
+ json: () => Promise.resolve(mockToken),
52
+ } as Response)
49
53
 
50
54
  await client.verifyTin('123', '456')
51
55
 
52
- expect(ofetch).toHaveBeenCalledWith(
53
- 'https://preprod-mytax.hasil.gov.my/connect/token',
56
+ expect(mockFetch).toHaveBeenCalledWith(
57
+ 'https://preprod-api.myinvois.hasil.gov.my/connect/token',
54
58
  expect.any(Object),
55
59
  )
56
60
  })
@@ -61,40 +65,45 @@ describe('MyInvoisClient', () => {
61
65
  expires_in: 3600,
62
66
  }
63
67
  vi.advanceTimersByTime(8000)
64
- vi.mocked(ofetch)
65
- .mockResolvedValueOnce(mockToken)
66
- .mockResolvedValueOnce(undefined)
68
+ mockFetch
69
+ .mockResolvedValueOnce({
70
+ ok: true,
71
+ json: () => Promise.resolve(mockToken),
72
+ } as Response)
73
+ .mockResolvedValueOnce({
74
+ ok: true,
75
+ json: () => Promise.resolve(undefined),
76
+ } as Response)
67
77
 
68
78
  await client.verifyTin('123', '456')
69
79
 
70
80
  // Check first call (token request)
71
- expect(ofetch).toHaveBeenNthCalledWith(
81
+ expect(mockFetch).toHaveBeenNthCalledWith(
72
82
  1,
73
- 'https://preprod-mytax.hasil.gov.my/connect/token',
83
+ 'https://preprod-api.myinvois.hasil.gov.my/connect/token',
74
84
  {
75
85
  method: 'POST',
76
86
  headers: {
77
87
  'Content-Type': 'application/x-www-form-urlencoded',
78
88
  },
79
- body: {
89
+ body: new URLSearchParams({
80
90
  grant_type: 'client_credentials',
81
91
  client_id: 'test-id',
82
92
  client_secret: 'test-secret',
83
93
  scope: 'InvoicingAPI',
84
- },
94
+ }),
85
95
  },
86
96
  )
87
97
 
88
98
  // Check second call (verifyTin request)
89
- expect(ofetch).toHaveBeenNthCalledWith(
99
+ expect(mockFetch).toHaveBeenNthCalledWith(
90
100
  2,
91
- `https://preprod-mytax.hasil.gov.my/api/v1.0/taxpayer/validate/123?idType=NRIC&idValue=456`,
101
+ `https://preprod-api.myinvois.hasil.gov.my/api/v1.0/taxpayer/validate/123?idType=NRIC&idValue=456`,
92
102
  {
93
103
  method: 'GET',
94
104
  headers: {
95
105
  Authorization: `Bearer ${mockToken.access_token}`,
96
106
  },
97
- responseType: 'json',
98
107
  },
99
108
  )
100
109
  })
@@ -105,7 +114,10 @@ describe('MyInvoisClient', () => {
105
114
  expires_in: 3600,
106
115
  }
107
116
 
108
- vi.mocked(ofetch).mockResolvedValueOnce(mockToken)
117
+ mockFetch.mockResolvedValueOnce({
118
+ ok: true,
119
+ json: () => Promise.resolve(mockToken),
120
+ } as Response)
109
121
 
110
122
  // First call to get token
111
123
  await client.verifyTin(process.env.TIN_VALUE!, process.env.NRIC_VALUE!)
@@ -116,8 +128,8 @@ describe('MyInvoisClient', () => {
116
128
  await client.verifyTin(process.env.TIN_VALUE!, process.env.NRIC_VALUE!)
117
129
 
118
130
  // Token endpoint should only be called once
119
- expect(ofetch).toHaveBeenCalledWith(
120
- 'https://preprod-mytax.hasil.gov.my/connect/token',
131
+ expect(mockFetch).toHaveBeenCalledWith(
132
+ 'https://preprod-api.myinvois.hasil.gov.my/connect/token',
121
133
  expect.any(Object),
122
134
  )
123
135
  })
@@ -130,15 +142,20 @@ describe('MyInvoisClient', () => {
130
142
  expires_in: 3600,
131
143
  }
132
144
 
133
- vi.mocked(ofetch)
134
- .mockResolvedValueOnce(mockToken) // Token call
135
- .mockResolvedValueOnce(undefined) // Verify call
145
+ mockFetch
146
+ .mockResolvedValueOnce({
147
+ ok: true,
148
+ json: () => Promise.resolve(mockToken),
149
+ } as Response)
150
+ .mockResolvedValueOnce({
151
+ ok: true,
152
+ json: () => Promise.resolve(undefined),
153
+ } as Response)
136
154
 
137
- const result = await client.verifyTin('123', '456')
138
- expect(result).toBe(true)
155
+ await client.verifyTin('123', '456')
139
156
 
140
- expect(ofetch).toHaveBeenCalledWith(
141
- `https://preprod-mytax.hasil.gov.my/api/v1.0/taxpayer/validate/123?idType=NRIC&idValue=456`,
157
+ expect(mockFetch).toHaveBeenCalledWith(
158
+ `https://preprod-api.myinvois.hasil.gov.my/api/v1.0/taxpayer/validate/123?idType=NRIC&idValue=456`,
142
159
  expect.objectContaining({
143
160
  method: 'GET',
144
161
  headers: {
@@ -154,9 +171,12 @@ describe('MyInvoisClient', () => {
154
171
  expires_in: 3600,
155
172
  }
156
173
 
157
- vi.mocked(ofetch)
158
- .mockResolvedValueOnce(mockToken) // Token call
159
- .mockRejectedValueOnce(new Error('Invalid TIN')) // Verify call
174
+ mockFetch
175
+ .mockResolvedValueOnce({
176
+ ok: true,
177
+ json: () => Promise.resolve(mockToken),
178
+ } as Response)
179
+ .mockRejectedValueOnce(new Error('Invalid TIN'))
160
180
 
161
181
  const result = await client.verifyTin(
162
182
  process.env.TIN_VALUE!,
package/vitest.config.ts CHANGED
@@ -2,4 +2,7 @@ import { defineConfig } from 'vitest/config'
2
2
 
3
3
  export default defineConfig({
4
4
  root: '.',
5
+ test: {
6
+ setupFiles: ['dotenv/config'],
7
+ },
5
8
  })