|
1 | 1 | import type { InstanceType } from '@clerk/shared/types'; |
2 | 2 | import { afterEach, beforeEach, describe, expect, it, type Mock, vi } from 'vitest'; |
3 | 3 |
|
4 | | -import { mockFetch, mockNetworkFailedFetch } from '@/test/core-fixtures'; |
| 4 | +import { mockFetch, mockJwt, mockNetworkFailedFetch } from '@/test/core-fixtures'; |
5 | 5 | import { debugLogger } from '@/utils/debug'; |
6 | 6 |
|
7 | 7 | import { SUPPORTED_FAPI_VERSION } from '../../constants'; |
@@ -44,7 +44,7 @@ describe('Token', () => { |
44 | 44 | }); |
45 | 45 |
|
46 | 46 | describe('with offline browser and network failure', () => { |
47 | | - let warnSpy; |
| 47 | + let warnSpy: ReturnType<typeof vi.spyOn>; |
48 | 48 |
|
49 | 49 | beforeEach(() => { |
50 | 50 | Object.defineProperty(window.navigator, 'onLine', { |
@@ -103,5 +103,54 @@ describe('Token', () => { |
103 | 103 | }); |
104 | 104 | }); |
105 | 105 | }); |
| 106 | + |
| 107 | + it('creates token successfully with valid response', async () => { |
| 108 | + mockFetch(true, 200, { jwt: mockJwt }); |
| 109 | + BaseResource.clerk = { getFapiClient: () => createFapiClient(baseFapiClientOptions) } as any; |
| 110 | + |
| 111 | + const token = await Token.create('/path/to/tokens', { organizationId: 'org_123' }); |
| 112 | + |
| 113 | + expect(global.fetch).toHaveBeenCalledTimes(1); |
| 114 | + const [url, options] = (global.fetch as Mock).mock.calls[0]; |
| 115 | + expect(url.toString()).toContain('https://clerk.example.com/v1/path/to/tokens'); |
| 116 | + expect(url.toString()).not.toContain('debug=skip_cache'); |
| 117 | + expect(options).toMatchObject({ |
| 118 | + body: 'organization_id=org_123', |
| 119 | + credentials: 'include', |
| 120 | + method: 'POST', |
| 121 | + }); |
| 122 | + expect(token).toBeInstanceOf(Token); |
| 123 | + expect(token.jwt).toBeDefined(); |
| 124 | + }); |
| 125 | + |
| 126 | + it('creates token with skipCache=false by default', async () => { |
| 127 | + mockFetch(true, 200, { jwt: mockJwt }); |
| 128 | + BaseResource.clerk = { getFapiClient: () => createFapiClient(baseFapiClientOptions) } as any; |
| 129 | + |
| 130 | + await Token.create('/path/to/tokens'); |
| 131 | + |
| 132 | + const [url] = (global.fetch as Mock).mock.calls[0]; |
| 133 | + expect(url.toString()).not.toContain('debug=skip_cache'); |
| 134 | + }); |
| 135 | + |
| 136 | + it('creates token with skipCache=true and includes query parameter', async () => { |
| 137 | + mockFetch(true, 200, { jwt: mockJwt }); |
| 138 | + BaseResource.clerk = { getFapiClient: () => createFapiClient(baseFapiClientOptions) } as any; |
| 139 | + |
| 140 | + await Token.create('/path/to/tokens', {}, true); |
| 141 | + |
| 142 | + const [url] = (global.fetch as Mock).mock.calls[0]; |
| 143 | + expect(url.toString()).toContain('debug=skip_cache'); |
| 144 | + }); |
| 145 | + |
| 146 | + it('creates token with skipCache=false explicitly and excludes query parameter', async () => { |
| 147 | + mockFetch(true, 200, { jwt: mockJwt }); |
| 148 | + BaseResource.clerk = { getFapiClient: () => createFapiClient(baseFapiClientOptions) } as any; |
| 149 | + |
| 150 | + await Token.create('/path/to/tokens', {}, false); |
| 151 | + |
| 152 | + const [url] = (global.fetch as Mock).mock.calls[0]; |
| 153 | + expect(url.toString()).not.toContain('debug=skip_cache'); |
| 154 | + }); |
106 | 155 | }); |
107 | 156 | }); |
0 commit comments