Skip to content

Commit 971c387

Browse files
committed
Add tests for fetch/set profile updates
1 parent 8fe9b7a commit 971c387

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

core/src/user/index.test.ts

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,121 @@ describe("NDKUser", () => {
9494
pubkey = user.pubkey;
9595
});
9696

97+
it("profile returns metadata event", async () => {
98+
const event = EventGenerator.createEvent(
99+
0,
100+
JSON.stringify({
101+
name: "Jeff",
102+
picture: "https://image.url",
103+
}),
104+
pubkey,
105+
);
106+
event.created_at = NOW_SEC - 7200;
107+
108+
ndk.fetchEvent = vi.fn().mockResolvedValueOnce(event);
109+
110+
const profile = await user.fetchProfile();
111+
const metadataevent = JSON.parse(profile.profileEvent);
112+
113+
expect(metadataevent.id).toEqual(event.id);
114+
});
115+
116+
it("duplicate fetching of profile", async () => {
117+
const event = EventGenerator.createEvent(
118+
0,
119+
JSON.stringify({
120+
name: "Jeff",
121+
picture: "https://image.url",
122+
}),
123+
pubkey,
124+
);
125+
event.created_at = NOW_SEC - 7200;
126+
127+
ndk.fetchEvent = vi.fn().mockResolvedValueOnce(event);
128+
129+
const profile = await user.fetchProfile();
130+
expect(profile?.name).toEqual("Jeff");
131+
expect(profile?.picture).toEqual("https://image.url");
132+
133+
const profile2 = await user.fetchProfile();
134+
expect(profile2?.name).toEqual("Jeff");
135+
expect(profile2?.picture).toEqual("https://image.url");
136+
});
137+
138+
it("newer profile overwrites older profile (two fetches)", async () => {
139+
oldEvent = EventGenerator.createEvent(
140+
0,
141+
JSON.stringify({
142+
name: "Jeff_OLD",
143+
picture: "https://image.url.old",
144+
about: "About jeff OLD",
145+
}),
146+
pubkey,
147+
);
148+
oldEvent.created_at = NOW_SEC - 7200;
149+
150+
ndk.fetchEvent = vi.fn().mockResolvedValueOnce(oldEvent);
151+
152+
await user.fetchProfile();
153+
expect(user.profile?.name).toEqual("Jeff_OLD");
154+
expect(user.profile?.picture).toEqual("https://image.url.old");
155+
expect(user.profile?.about).toEqual("About jeff OLD");
156+
157+
newEvent = EventGenerator.createEvent(
158+
0,
159+
JSON.stringify({
160+
name: "Jeff",
161+
picture: "https://image.url",
162+
about: "About jeff",
163+
}),
164+
pubkey,
165+
);
166+
newEvent.created_at = NOW_SEC;
167+
168+
ndk.fetchEvent = vi.fn().mockResolvedValueOnce(newEvent);
169+
await user.fetchProfile();
170+
expect(user.profile?.name).toEqual("Jeff");
171+
expect(user.profile?.picture).toEqual("https://image.url");
172+
expect(user.profile?.about).toEqual("About jeff");
173+
});
174+
175+
it.skip("older profile does not overwrite newer profile (two fetches)", async () => {
176+
newEvent = EventGenerator.createEvent(
177+
0,
178+
JSON.stringify({
179+
name: "Jeff",
180+
picture: "https://image.url",
181+
about: "About jeff",
182+
}),
183+
pubkey,
184+
);
185+
newEvent.created_at = NOW_SEC - 3600;
186+
187+
oldEvent = EventGenerator.createEvent(
188+
0,
189+
JSON.stringify({
190+
name: "Jeff_OLD",
191+
picture: "https://image.url.old",
192+
about: "About jeff OLD",
193+
}),
194+
pubkey,
195+
);
196+
oldEvent.created_at = NOW_SEC - 7200;
197+
198+
ndk.fetchEvent = vi.fn().mockResolvedValue(newEvent);
199+
200+
await user.fetchProfile();
201+
expect(user.profile?.name).toEqual("Jeff");
202+
expect(user.profile?.picture).toEqual("https://image.url");
203+
expect(user.profile?.about).toEqual("About jeff");
204+
205+
ndk.fetchEvent = vi.fn().mockResolvedValue(oldEvent);
206+
await user.fetchProfile();
207+
expect(user.profile?.name).toEqual("Jeff");
208+
expect(user.profile?.picture).toEqual("https://image.url");
209+
expect(user.profile?.about).toEqual("About jeff");
210+
});
211+
97212
it("Returns updated fields", async () => {
98213
// Use EventGenerator to create profile events
99214
newEvent = EventGenerator.createEvent(

0 commit comments

Comments
 (0)