Skip to content

Commit ff0b51b

Browse files
committed
refactor: replace isOn with useFlag in SvelteLDClient and update tests
1 parent a4b1f3f commit ff0b51b

File tree

4 files changed

+56
-98
lines changed

4 files changed

+56
-98
lines changed

packages/sdk/svelte/__tests__/lib/client/SvelteLDClient.test.ts

Lines changed: 41 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe('launchDarkly', () => {
3333
expect(ld).toHaveProperty('initialize');
3434
expect(ld).toHaveProperty('initializing');
3535
expect(ld).toHaveProperty('watch');
36-
expect(ld).toHaveProperty('isOn');
36+
expect(ld).toHaveProperty('useFlag');
3737
});
3838

3939
describe('initialize', async () => {
@@ -55,7 +55,7 @@ describe('launchDarkly', () => {
5555
const flagKey = 'test-flag';
5656
const user = { key: 'user1' };
5757

58-
expect(() => ld.isOn(flagKey)).toThrow('LaunchDarkly client not initialized');
58+
expect(() => ld.useFlag(flagKey, true)).toThrow('LaunchDarkly client not initialized');
5959
await expect(() => ld.identify(user)).rejects.toThrow(
6060
'LaunchDarkly client not initialized',
6161
);
@@ -162,98 +162,54 @@ describe('launchDarkly', () => {
162162
});
163163
});
164164

165-
// TODO: fix these tests
166-
// describe('isOn function', () => {
167-
// const ld = LD;
168-
169-
// beforeEach(() => {
170-
// // mocks the initialize function to return the mockLDClient
171-
// (initialize as Mock<typeof initialize>).mockReturnValue(
172-
// mockLDClient as unknown as LDClient,
173-
// );
174-
// });
175-
176-
// afterEach(() => {
177-
// vi.clearAllMocks();
178-
// mockLDEventEmitter.removeAllListeners();
179-
// });
180-
181-
// it('should return true if the flag is on', () => {
182-
// const flagKey = 'test-flag';
183-
// ld.initialize(clientSideID);
184-
185-
// expect(ld.isOn(flagKey)).toBe(true);
186-
// });
187-
188-
// it('should return false if the flag is off', () => {
189-
// const flagKey = 'test-flag';
190-
// ld.initialize(clientSideID);
191-
192-
// mockAllFlags.mockReturnValue({ ...rawFlags, 'test-flag': false });
193-
194-
// // dispatch a change event on ldClient
195-
// const changeCallback = mockLDClient.on.mock.calls[0][1];
196-
// changeCallback();
197-
198-
// expect(ld.isOn(flagKey)).toBe(false);
199-
// });
200-
201-
// it('should return false if the flag is not found', () => {
202-
// const flagKey = 'non-existent-flag';
203-
// ld.initialize(clientSideID, { key: 'user1' });
204-
205-
// expect(ld.isOn(flagKey)).toBe(false);
206-
// });
207-
// });
208-
209-
// describe('identify function', () => {
210-
// const ld = LD;
211-
// beforeEach(() => {
212-
// mockInitialize.mockImplementation(() => mockLDClient);
213-
// mockAllFlags.mockImplementation(() => rawFlags);
214-
// });
215-
216-
// it('should call the identify method on the LaunchDarkly client', () => {
217-
// const user = { key: 'user1' };
218-
// ld.initialize(clientSideID, user);
219-
220-
// ld.identify(user);
221-
222-
// expect(mockLDClient.identify).toHaveBeenCalledWith(user);
223-
// });
224-
// });
165+
describe('useFlag function', () => {
166+
const ld = LD;
225167

226-
// describe('flags store', () => {
227-
// const ld = LD;
228-
// beforeEach(() => {
229-
// mockInitialize.mockImplementation(() => mockLDClient);
230-
// mockAllFlags.mockImplementation(() => rawFlags);
231-
// });
168+
beforeEach(() => {
169+
// mocks the initialize function to return the mockLDClient
170+
(initialize as Mock<typeof initialize>).mockReturnValue(
171+
mockLDClient as unknown as LDClient,
172+
);
173+
});
232174

233-
// it('should return a readonly store of the flags', () => {
234-
// ld.initialize(clientSideID, { key: 'user1' });
175+
afterEach(() => {
176+
vi.clearAllMocks();
177+
mockLDEventEmitter.removeAllListeners();
178+
});
235179

236-
// const { flags } = ld;
180+
it('should return flag value', () => {
181+
mockLDClient.variation.mockReturnValue(true);
182+
const flagKey = 'test-flag';
183+
ld.initialize(clientSideID, mockContext);
237184

238-
// expect(get(flags)).toEqual(rawFlags);
239-
// });
185+
expect(ld.useFlag(flagKey, false)).toBe(true);
186+
expect(mockLDClient.variation).toHaveBeenCalledWith(flagKey, false);
187+
});
188+
});
240189

241-
// it('should update the flags store when the flags change', () => {
242-
// ld.initialize(clientSideID, { key: 'user1' });
190+
describe('identify function', () => {
191+
const ld = LD;
243192

244-
// const { flags } = ld;
193+
beforeEach(() => {
194+
// mocks the initialize function to return the mockLDClient
195+
(initialize as Mock<typeof initialize>).mockReturnValue(
196+
mockLDClient as unknown as LDClient,
197+
);
198+
});
245199

246-
// expect(get(flags)).toEqual(rawFlags);
200+
afterEach(() => {
201+
vi.clearAllMocks();
202+
mockLDEventEmitter.removeAllListeners();
203+
});
247204

248-
// const newFlags = { 'test-flag': false, 'another-test-flag': true };
249-
// mockAllFlags.mockReturnValue(newFlags);
205+
it('should call the identify method on the LaunchDarkly client', () => {
206+
const user = { key: 'user1' };
207+
ld.initialize(clientSideID, user);
250208

251-
// // dispatch a change event on ldClient
252-
// const changeCallback = mockLDClient.on.mock.calls[0][1];
253-
// changeCallback();
209+
ld.identify(user);
254210

255-
// expect(get(flags)).toEqual(newFlags);
256-
// });
257-
// });
211+
expect(mockLDClient.identify).toHaveBeenCalledWith(user);
212+
});
213+
});
258214
});
259215
});

packages/sdk/svelte/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
"check": "yarn prettier && yarn lint && yarn build && yarn test",
4141
"test": "playwright test",
4242
"test:unit": "vitest",
43-
"test:unit-ui": "vitest --ui"
43+
"test:unit-ui": "vitest --ui",
44+
"test:unit-coverage": "vitest --coverage"
4445
},
4546
"peerDependencies": {
4647
"@launchdarkly/js-client-sdk": "workspace:^",
@@ -60,6 +61,7 @@
6061
"@types/jest": "^29.5.11",
6162
"@typescript-eslint/eslint-plugin": "^6.20.0",
6263
"@typescript-eslint/parser": "^6.20.0",
64+
"@vitest/coverage-v8": "^2.1.8",
6365
"@vitest/ui": "^2.1.8",
6466
"eslint": "^8.45.0",
6567
"eslint-config-airbnb-base": "^15.0.0",

packages/sdk/svelte/src/lib/LDFlag.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<script lang="ts">
2-
import { LD, type LDFlagsValue } from './client/SvelteLDClient.js';
2+
import { LD, type LDFlagValue } from './client/SvelteLDClient.js';
33
44
export let flag: string;
5-
export let matches: LDFlagsValue = true;
5+
export let matches: LDFlagValue = true;
66
77
$: flagValue = LD.watch(flag);
88
</script>

packages/sdk/svelte/src/lib/client/SvelteLDClient.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { derived, get, type Readable, readonly, writable, type Writable } from 'svelte/store';
1+
import { derived, type Readable, readonly, writable, type Writable } from 'svelte/store';
22

33
import type { LDFlagSet } from '@launchdarkly/js-client-sdk';
44
import {
@@ -8,7 +8,7 @@ import {
88
type LDFlagValue,
99
} from '@launchdarkly/js-client-sdk/compat';
1010

11-
export type { LDContext };
11+
export type { LDContext, LDFlagValue };
1212

1313
/** Client ID for LaunchDarkly */
1414
export type LDClientID = string;
@@ -112,23 +112,23 @@ function createLD() {
112112
derived<Writable<LDFlags>, LDFlagValue>(flagsWritable, ($flags) => $flags[flagKey]);
113113

114114
/**
115-
* Checks if a flag is on.
116-
* @param {string} flagKey - The key of the flag to check.
117-
* @returns {boolean} True if the flag is on, false otherwise.
115+
* Gets the current value of a flag.
116+
* @param {string} flagKey - The key of the flag to get.
117+
* @param {TFlag} defaultValue - The default value of the flag.
118+
* @returns {TFlag} The current value of the flag.
118119
*/
119-
const isOn = (flagKey: string): boolean => {
120+
function useFlag<TFlag extends LDFlagValue>(flagKey: string, defaultValue: TFlag): TFlag {
120121
isClientInitialized(coreLdClient);
121-
const currentFlags = get(flagsWritable);
122-
return !!currentFlags[flagKey];
123-
};
122+
return coreLdClient.variation(flagKey, defaultValue);
123+
}
124124

125125
return {
126126
identify,
127127
flags: readonly(flagsWritable),
128128
initialize: LDInitialize,
129129
initializing: readonly(loading),
130130
watch,
131-
isOn,
131+
useFlag,
132132
};
133133
}
134134

0 commit comments

Comments
 (0)