Skip to content

Commit 2c43bb3

Browse files
Clean out all stale data from pop up
1 parent 43b3eb2 commit 2c43bb3

File tree

2 files changed

+86
-4
lines changed

2 files changed

+86
-4
lines changed

apps/browser/src/vault/popup/components/vault-v2/add-edit/add-edit-v2.component.spec.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,4 +381,88 @@ describe("AddEditV2Component", () => {
381381
expect(navigate).toHaveBeenCalledWith(["/tabs/vault"]);
382382
});
383383
});
384+
385+
describe("reloadAddEditCipherData", () => {
386+
beforeEach(fakeAsync(() => {
387+
addEditCipherInfo$.next({
388+
cipher: {
389+
name: "InitialName",
390+
type: CipherType.Login,
391+
login: {
392+
password: "initialPassword",
393+
username: "initialUsername",
394+
uris: [{ uri: "https://initial.com" }],
395+
},
396+
},
397+
} as AddEditCipherInfo);
398+
queryParams$.next({});
399+
tick();
400+
401+
cipherServiceMock.setAddEditCipherInfo.mockClear();
402+
}));
403+
404+
it("replaces all initialValues with new data, clearing stale fields", fakeAsync(() => {
405+
const newCipherInfo = {
406+
cipher: {
407+
name: "UpdatedName",
408+
type: CipherType.Login,
409+
login: {
410+
password: "updatedPassword",
411+
uris: [{ uri: "https://updated.com" }],
412+
},
413+
},
414+
} as AddEditCipherInfo;
415+
416+
addEditCipherInfo$.next(newCipherInfo);
417+
418+
const messageListener = component["messageListener"];
419+
messageListener({ command: "reloadAddEditCipherData" });
420+
tick();
421+
422+
expect(component.config.initialValues).toEqual({
423+
name: "UpdatedName",
424+
password: "updatedPassword",
425+
loginUri: "https://updated.com",
426+
} as OptionalInitialValues);
427+
428+
expect(cipherServiceMock.setAddEditCipherInfo).toHaveBeenCalledWith(null, "UserId");
429+
}));
430+
431+
it("does not reload data if config is not set", fakeAsync(() => {
432+
component.config = null;
433+
434+
const messageListener = component["messageListener"];
435+
messageListener({ command: "reloadAddEditCipherData" });
436+
tick();
437+
438+
expect(cipherServiceMock.setAddEditCipherInfo).not.toHaveBeenCalled();
439+
}));
440+
441+
it("does not reload data if latestCipherInfo is null", fakeAsync(() => {
442+
addEditCipherInfo$.next(null);
443+
444+
const messageListener = component["messageListener"];
445+
messageListener({ command: "reloadAddEditCipherData" });
446+
tick();
447+
448+
expect(component.config.initialValues).toEqual({
449+
name: "InitialName",
450+
password: "initialPassword",
451+
username: "initialUsername",
452+
loginUri: "https://initial.com",
453+
} as OptionalInitialValues);
454+
455+
expect(cipherServiceMock.setAddEditCipherInfo).not.toHaveBeenCalled();
456+
}));
457+
458+
it("ignores messages with different commands", fakeAsync(() => {
459+
const initialValues = component.config.initialValues;
460+
461+
const messageListener = component["messageListener"];
462+
messageListener({ command: "someOtherCommand" });
463+
tick();
464+
465+
expect(component.config.initialValues).toBe(initialValues);
466+
}));
467+
});
384468
});

apps/browser/src/vault/popup/components/vault-v2/add-edit/add-edit-v2.component.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ export class AddEditV2Component implements OnInit, OnDestroy {
226226

227227
/**
228228
* Reloads the cipher data when the popup is already open and new form data is submitted.
229+
* This completely replaces the initialValues to clear any stale data from the previous submission.
229230
*/
230231
private async reloadCipherData() {
231232
if (!this.config) {
@@ -241,10 +242,7 @@ export class AddEditV2Component implements OnInit, OnDestroy {
241242
if (latestCipherInfo != null) {
242243
this.config = {
243244
...this.config,
244-
initialValues: {
245-
...this.config.initialValues,
246-
...mapAddEditCipherInfoToInitialValues(latestCipherInfo),
247-
},
245+
initialValues: mapAddEditCipherInfoToInitialValues(latestCipherInfo),
248246
};
249247

250248
// Be sure to clear the "cached" cipher info, so it doesn't get used again

0 commit comments

Comments
 (0)