Skip to content

Commit a960ab1

Browse files
committed
Fix formatting
1 parent 6dbb398 commit a960ab1

File tree

6 files changed

+110
-79
lines changed

6 files changed

+110
-79
lines changed

src/frontend/src/lib/constants/store.constants.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ export const storeLocalStorageKey = {
22
LastUsedIdentities: "ii-last-used-identities",
33
} as const;
44

5-
export type StoreLocalStorageKey = (typeof storeLocalStorageKey)[keyof typeof storeLocalStorageKey]
5+
export type StoreLocalStorageKey =
6+
(typeof storeLocalStorageKey)[keyof typeof storeLocalStorageKey];

src/frontend/src/lib/stores/last-used-identities.store.test.ts

+55-36
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
1-
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2-
import { get } from 'svelte/store';
1+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
2+
import { get } from "svelte/store";
33

44
// Mock the dependency: writableStored
5-
vi.mock('$app/environment', () => ({
6-
browser: true // Or false, depending on the test case
5+
vi.mock("$app/environment", () => ({
6+
browser: true, // Or false, depending on the test case
77
}));
88

9-
109
// Import the stores AFTER mocking the dependency
11-
import { lastUsedIdentitiesStore, lastUsedIdentityStore } from './last-used-identities.store';
12-
import type { LastUsedIdentity, LastUsedIdentitiesData } from './last-used-identities.store';
13-
14-
describe('lastUsedIdentitiesStore', () => {
10+
import {
11+
lastUsedIdentitiesStore,
12+
lastUsedIdentityStore,
13+
} from "./last-used-identities.store";
14+
import type {
15+
LastUsedIdentity,
16+
LastUsedIdentitiesData,
17+
} from "./last-used-identities.store";
18+
19+
describe("lastUsedIdentitiesStore", () => {
1520
const mockTimestamp1 = 1700000000000;
1621
const mockTimestamp2 = 1700000001000;
1722
const mockTimestamp3 = 1700000002000;
1823

19-
const identity1 = BigInt('111');
20-
const name1 = 'Test ID 1';
21-
const identity2 = BigInt('222');
22-
const name2 = 'Test ID 2';
24+
const identity1 = BigInt("111");
25+
const name1 = "Test ID 1";
26+
const identity2 = BigInt("222");
27+
const name2 = "Test ID 2";
2328

2429
beforeEach(() => {
2530
// Reset the store state and time before each test
@@ -33,11 +38,11 @@ describe('lastUsedIdentitiesStore', () => {
3338
vi.useRealTimers();
3439
});
3540

36-
it('should initialize with an empty object', () => {
41+
it("should initialize with an empty object", () => {
3742
expect(get(lastUsedIdentitiesStore)).toEqual({});
3843
});
3944

40-
it('should add the first identity correctly', () => {
45+
it("should add the first identity correctly", () => {
4146
lastUsedIdentitiesStore.addLatestUsed(identity1, name1);
4247

4348
const expected: LastUsedIdentitiesData = {
@@ -50,7 +55,7 @@ describe('lastUsedIdentitiesStore', () => {
5055
expect(get(lastUsedIdentitiesStore)).toEqual(expected);
5156
});
5257

53-
it('should add multiple identities with correct timestamps', () => {
58+
it("should add multiple identities with correct timestamps", () => {
5459
// Add first identity
5560
lastUsedIdentitiesStore.addLatestUsed(identity1, name1);
5661

@@ -59,16 +64,27 @@ describe('lastUsedIdentitiesStore', () => {
5964
lastUsedIdentitiesStore.addLatestUsed(identity2, name2);
6065

6166
const expected: LastUsedIdentitiesData = {
62-
[identity1.toString()]: { identityNumber: identity1, name: name1, lastUsedTimestampMillis: mockTimestamp1 },
63-
[identity2.toString()]: { identityNumber: identity2, name: name2, lastUsedTimestampMillis: mockTimestamp2 },
67+
[identity1.toString()]: {
68+
identityNumber: identity1,
69+
name: name1,
70+
lastUsedTimestampMillis: mockTimestamp1,
71+
},
72+
[identity2.toString()]: {
73+
identityNumber: identity2,
74+
name: name2,
75+
lastUsedTimestampMillis: mockTimestamp2,
76+
},
6477
};
6578
expect(get(lastUsedIdentitiesStore)).toEqual(expected);
6679
});
6780

68-
it('should update the timestamp when adding an existing identity', () => {
81+
it("should update the timestamp when adding an existing identity", () => {
6982
// Add identity initially
7083
lastUsedIdentitiesStore.addLatestUsed(identity1, name1);
71-
expect(get(lastUsedIdentitiesStore)[identity1.toString()].lastUsedTimestampMillis).toBe(mockTimestamp1);
84+
expect(
85+
get(lastUsedIdentitiesStore)[identity1.toString()]
86+
.lastUsedTimestampMillis,
87+
).toBe(mockTimestamp1);
7288

7389
// Advance time and add the same identity again
7490
vi.setSystemTime(mockTimestamp3);
@@ -82,10 +98,13 @@ describe('lastUsedIdentitiesStore', () => {
8298
},
8399
};
84100
expect(get(lastUsedIdentitiesStore)).toEqual(expected);
85-
expect(get(lastUsedIdentitiesStore)[identity1.toString()].lastUsedTimestampMillis).toBe(mockTimestamp3);
101+
expect(
102+
get(lastUsedIdentitiesStore)[identity1.toString()]
103+
.lastUsedTimestampMillis,
104+
).toBe(mockTimestamp3);
86105
});
87106

88-
it('should reset the store to an empty object', () => {
107+
it("should reset the store to an empty object", () => {
89108
lastUsedIdentitiesStore.addLatestUsed(identity1, name1);
90109
expect(get(lastUsedIdentitiesStore)).not.toEqual({}); // Ensure it's not empty
91110

@@ -94,17 +113,17 @@ describe('lastUsedIdentitiesStore', () => {
94113
});
95114
});
96115

97-
describe('lastUsedIdentityStore (derived)', () => {
116+
describe("lastUsedIdentityStore (derived)", () => {
98117
const mockTimestamp1 = 1700000000000;
99118
const mockTimestamp2 = 1700000001000;
100119
const mockTimestamp3 = 1700000002000;
101120

102-
const identity1 = BigInt('101');
103-
const name1 = 'Derived ID 1';
104-
const identity2 = BigInt('202');
105-
const name2 = 'Derived ID 2';
106-
const identity3 = BigInt('303');
107-
const name3 = 'Derived ID 3';
121+
const identity1 = BigInt("101");
122+
const name1 = "Derived ID 1";
123+
const identity2 = BigInt("202");
124+
const name2 = "Derived ID 2";
125+
const identity3 = BigInt("303");
126+
const name3 = "Derived ID 3";
108127

109128
beforeEach(() => {
110129
vi.useFakeTimers();
@@ -117,11 +136,11 @@ describe('lastUsedIdentityStore (derived)', () => {
117136
vi.useRealTimers();
118137
});
119138

120-
it('should be undefined when the source store is empty', () => {
139+
it("should be undefined when the source store is empty", () => {
121140
expect(get(lastUsedIdentityStore)).toBeUndefined();
122141
});
123142

124-
it('should return the only identity when one is added', () => {
143+
it("should return the only identity when one is added", () => {
125144
lastUsedIdentitiesStore.addLatestUsed(identity1, name1);
126145

127146
const expected: LastUsedIdentity = {
@@ -132,7 +151,7 @@ describe('lastUsedIdentityStore (derived)', () => {
132151
expect(get(lastUsedIdentityStore)).toEqual(expected);
133152
});
134153

135-
it('should return the latest identity when multiple are added', () => {
154+
it("should return the latest identity when multiple are added", () => {
136155
vi.setSystemTime(mockTimestamp1);
137156
lastUsedIdentitiesStore.addLatestUsed(identity2, name2);
138157

@@ -149,15 +168,15 @@ describe('lastUsedIdentityStore (derived)', () => {
149168
// Add identity 3 (at time 3) - Should become the latest
150169
vi.setSystemTime(mockTimestamp3);
151170
lastUsedIdentitiesStore.addLatestUsed(identity3, name3);
152-
const expectedNewest: LastUsedIdentity = {
171+
const expectedNewest: LastUsedIdentity = {
153172
identityNumber: identity3,
154173
name: name3,
155174
lastUsedTimestampMillis: mockTimestamp3,
156175
};
157176
expect(get(lastUsedIdentityStore)).toEqual(expectedNewest);
158177
});
159178

160-
it('should update when an existing identity becomes the latest again', () => {
179+
it("should update when an existing identity becomes the latest again", () => {
161180
// Add 1 at time 1
162181
lastUsedIdentitiesStore.addLatestUsed(identity1, name1);
163182

@@ -178,11 +197,11 @@ describe('lastUsedIdentityStore (derived)', () => {
178197
expect(get(lastUsedIdentityStore)).toEqual(expected);
179198
});
180199

181-
it('should become undefined after the source store is reset', () => {
200+
it("should become undefined after the source store is reset", () => {
182201
lastUsedIdentitiesStore.addLatestUsed(identity1, name1);
183202
expect(get(lastUsedIdentityStore)).toBeDefined();
184203

185204
lastUsedIdentitiesStore.reset();
186205
expect(get(lastUsedIdentityStore)).toBeUndefined();
187206
});
188-
});
207+
});

src/frontend/src/lib/stores/last-used-identities.store.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export type LastUsedIdentitiesData = {
1313
type LastUsedIdentitiesStore = Readable<LastUsedIdentitiesData> & {
1414
addLatestUsed: (identityNumber: bigint, name: string) => void;
1515
reset: () => void;
16-
}
16+
};
1717

1818
export const initLastUsedIdentitiesStore = (): LastUsedIdentitiesStore => {
1919
const { subscribe, set, update } = writableStored<LastUsedIdentitiesData>({
@@ -37,11 +37,16 @@ export const initLastUsedIdentitiesStore = (): LastUsedIdentitiesStore => {
3737
reset: () => {
3838
set({});
3939
},
40-
}
40+
};
4141
};
4242

4343
export const lastUsedIdentitiesStore = initLastUsedIdentitiesStore();
4444

45-
export const lastUsedIdentityStore: Readable<LastUsedIdentity> = derived(lastUsedIdentitiesStore, (lastUsedIdentities) => {
46-
return Object.values(lastUsedIdentities).sort((a, b) => b.lastUsedTimestampMillis - a.lastUsedTimestampMillis)[0];
47-
});
45+
export const lastUsedIdentityStore: Readable<LastUsedIdentity> = derived(
46+
lastUsedIdentitiesStore,
47+
(lastUsedIdentities) => {
48+
return Object.values(lastUsedIdentities).sort(
49+
(a, b) => b.lastUsedTimestampMillis - a.lastUsedTimestampMillis,
50+
)[0];
51+
},
52+
);

src/frontend/src/lib/stores/writable.store.test.ts

+23-20
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { get } from "svelte/store";
44
import type { LastUsedIdentity } from "$lib/stores/last-used-identities.store";
55
import { jsonReplacer } from "@dfinity/utils";
66

7-
vi.mock('$app/environment', () => ({
8-
browser: true // Or false, depending on the test case
7+
vi.mock("$app/environment", () => ({
8+
browser: true, // Or false, depending on the test case
99
}));
1010

1111
// Mock data based on LastUsedIdentity type
@@ -27,7 +27,7 @@ const stringigyJson = (data: any) => JSON.stringify(data, jsonReplacer);
2727
describe("writableStored", () => {
2828
beforeEach(() => {
2929
localStorage.clear();
30-
})
30+
});
3131

3232
it("writes to local storage when state changes", () => {
3333
const store = writableStored<LastUsedIdentity>({
@@ -39,15 +39,15 @@ describe("writableStored", () => {
3939
store.set(newState);
4040

4141
expect(
42-
window.localStorage.getItem(storeLocalStorageKey.LastUsedIdentities)
42+
window.localStorage.getItem(storeLocalStorageKey.LastUsedIdentities),
4343
).toEqual(stringigyJson(newState));
4444
});
4545

4646
it("loads initial value from local storage if present", () => {
4747
const storedState = mockIdentity2;
4848
window.localStorage.setItem(
4949
storeLocalStorageKey.LastUsedIdentities,
50-
stringigyJson(storedState)
50+
stringigyJson(storedState),
5151
);
5252
const store = writableStored<LastUsedIdentity>({
5353
key: storeLocalStorageKey.LastUsedIdentities,
@@ -68,7 +68,8 @@ describe("writableStored", () => {
6868
});
6969

7070
it("should serialize bigint values", () => {
71-
const defaultValue = BigInt(1000000000000000000000000000000000000000000000000000000);
71+
const defaultValue =
72+
BigInt(1000000000000000000000000000000000000000000000000000000);
7273
const store = writableStored({
7374
key: storeLocalStorageKey.LastUsedIdentities,
7475
defaultValue,
@@ -88,7 +89,7 @@ describe("writableStored", () => {
8889
const defaultValue = mockIdentity1;
8990
window.localStorage.setItem(
9091
storeLocalStorageKey.LastUsedIdentities,
91-
stringigyJson({ data: storedState, version: 0 })
92+
stringigyJson({ data: storedState, version: 0 }),
9293
);
9394
const store = writableStored<LastUsedIdentity>({
9495
key: storeLocalStorageKey.LastUsedIdentities,
@@ -104,7 +105,7 @@ describe("writableStored", () => {
104105
const defaultValue = mockIdentity1;
105106
window.localStorage.setItem(
106107
storeLocalStorageKey.LastUsedIdentities,
107-
stringigyJson({ data: storedState, version: 2 })
108+
stringigyJson({ data: storedState, version: 2 }),
108109
);
109110
const store = writableStored<LastUsedIdentity>({
110111
key: storeLocalStorageKey.LastUsedIdentities,
@@ -114,12 +115,12 @@ describe("writableStored", () => {
114115

115116
expect(get(store)).toEqual(defaultValue);
116117
expect(
117-
localStorage.getItem(storeLocalStorageKey.LastUsedIdentities)
118+
localStorage.getItem(storeLocalStorageKey.LastUsedIdentities),
118119
).toEqual(
119120
stringigyJson({
120121
data: defaultValue,
121122
version: 1,
122-
})
123+
}),
123124
);
124125
});
125126

@@ -128,16 +129,17 @@ describe("writableStored", () => {
128129
const defaultValue = mockIdentity1;
129130
window.localStorage.setItem(
130131
storeLocalStorageKey.LastUsedIdentities,
131-
stringigyJson({ data: storedState, version: 2 }) // Stored data has a version
132+
stringigyJson({ data: storedState, version: 2 }), // Stored data has a version
132133
);
133-
const store = writableStored<LastUsedIdentity>({ // New store definition has no version
134+
const store = writableStored<LastUsedIdentity>({
135+
// New store definition has no version
134136
key: storeLocalStorageKey.LastUsedIdentities,
135137
defaultValue,
136138
});
137139

138140
expect(get(store)).toEqual(defaultValue);
139141
expect(
140-
localStorage.getItem(storeLocalStorageKey.LastUsedIdentities)
142+
localStorage.getItem(storeLocalStorageKey.LastUsedIdentities),
141143
).toEqual(stringigyJson(defaultValue)); // Expect default value because stored had version, new didn't
142144
});
143145

@@ -146,9 +148,10 @@ describe("writableStored", () => {
146148
const defaultValue = mockIdentity1;
147149
window.localStorage.setItem(
148150
storeLocalStorageKey.LastUsedIdentities,
149-
stringigyJson(storedState) // No version in stored data
151+
stringigyJson(storedState), // No version in stored data
150152
);
151-
const store = writableStored<LastUsedIdentity>({ // No version in store definition
153+
const store = writableStored<LastUsedIdentity>({
154+
// No version in store definition
152155
key: storeLocalStorageKey.LastUsedIdentities,
153156
defaultValue,
154157
});
@@ -161,7 +164,7 @@ describe("writableStored", () => {
161164
const defaultValue = mockIdentity1;
162165
window.localStorage.setItem(
163166
storeLocalStorageKey.LastUsedIdentities,
164-
stringigyJson({ data: storedState, version: 5 })
167+
stringigyJson({ data: storedState, version: 5 }),
165168
);
166169
const store = writableStored<LastUsedIdentity>({
167170
key: storeLocalStorageKey.LastUsedIdentities,
@@ -183,13 +186,13 @@ describe("writableStored", () => {
183186
const newState = mockIdentity2;
184187
store.set(newState);
185188
expect(
186-
window.localStorage.getItem(storeLocalStorageKey.LastUsedIdentities)
189+
window.localStorage.getItem(storeLocalStorageKey.LastUsedIdentities),
187190
).toEqual(stringigyJson(newState));
188191

189192
store.unsubscribeStorage();
190193
store.set(defaultValue);
191194
expect(
192-
window.localStorage.getItem(storeLocalStorageKey.LastUsedIdentities)
195+
window.localStorage.getItem(storeLocalStorageKey.LastUsedIdentities),
193196
).toEqual(stringigyJson(newState));
194197
});
195198
});
@@ -203,7 +206,7 @@ describe("writableStored", () => {
203206
});
204207

205208
expect(get(store)).toEqual(defaultState);
206-
209+
207210
const newState = mockIdentity2;
208211

209212
store.set(newState);
@@ -212,4 +215,4 @@ describe("writableStored", () => {
212215
store.resetForTesting();
213216
expect(get(store)).toEqual(defaultState);
214217
});
215-
});
218+
});

0 commit comments

Comments
 (0)