Skip to content

Commit 7037be5

Browse files
committed
update query string param construction and add test
1 parent 12c03b4 commit 7037be5

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

packages/clerk-js/src/core/resources/Token.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import type { JWT, TokenJSON, TokenJSONSnapshot, TokenResource } from '@clerk/shared/types';
22

3-
import { decode } from '../../utils';
4-
import { CLERK_SKIP_CACHE } from '../constants';
5-
import { BaseResource } from './internal';
3+
import { CLERK_SKIP_CACHE } from '@/core/constants';
4+
import { decode } from '@/utils';
5+
6+
import { BaseResource } from './Base';
67

78
export class Token extends BaseResource implements TokenResource {
89
pathRoot = 'tokens';
910

1011
jwt?: JWT;
1112

1213
static async create(path: string, body: any = {}, skipCache = false): Promise<TokenResource> {
13-
const search = skipCache ? { [CLERK_SKIP_CACHE]: 'true' } : undefined;
14+
const search = skipCache ? `${CLERK_SKIP_CACHE}=true` : undefined;
1415

1516
const json = (await BaseResource._fetch<TokenJSON>({
1617
body,

packages/clerk-js/src/core/resources/__tests__/Token.test.ts

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type { InstanceType } from '@clerk/shared/types';
22
import { afterEach, beforeEach, describe, expect, it, type Mock, vi } from 'vitest';
33

4-
import { mockFetch, mockNetworkFailedFetch } from '@/test/core-fixtures';
4+
import { mockFetch, mockJwt, mockNetworkFailedFetch } from '@/test/core-fixtures';
55
import { debugLogger } from '@/utils/debug';
66

7-
import { SUPPORTED_FAPI_VERSION } from '../../constants';
7+
import { CLERK_SKIP_CACHE, SUPPORTED_FAPI_VERSION } from '../../constants';
88
import { createFapiClient } from '../../fapiClient';
99
import { BaseResource } from '../internal';
1010
import { Token } from '../Token';
@@ -44,7 +44,7 @@ describe('Token', () => {
4444
});
4545

4646
describe('with offline browser and network failure', () => {
47-
let warnSpy;
47+
let warnSpy: ReturnType<typeof vi.spyOn>;
4848

4949
beforeEach(() => {
5050
Object.defineProperty(window.navigator, 'onLine', {
@@ -103,5 +103,54 @@ describe('Token', () => {
103103
});
104104
});
105105
});
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(CLERK_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(CLERK_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(`${CLERK_SKIP_CACHE}=true`);
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(CLERK_SKIP_CACHE);
154+
});
106155
});
107156
});

0 commit comments

Comments
 (0)