1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-18 17:23:37 +00:00

[PM-9190] Use updateFn for patchCipher so that the current CipherView is available for context (#10258)

This commit is contained in:
Shane Melton
2024-07-25 07:50:39 -07:00
committed by GitHub
parent 14f51544c7
commit f4023762a8
13 changed files with 175 additions and 124 deletions

View File

@@ -62,9 +62,11 @@ describe("CardDetailsSectionComponent", () => {
cardView.number = "4242 4242 4242 4242";
cardView.brand = "Visa";
expect(patchCipherSpy).toHaveBeenCalledWith({
card: cardView,
});
expect(patchCipherSpy).toHaveBeenCalled();
const patchFn = patchCipherSpy.mock.lastCall[0];
const updateCipher = patchFn(new CipherView());
expect(updateCipher.card).toEqual(cardView);
});
it("it converts the year integer to a string", () => {
@@ -75,9 +77,11 @@ describe("CardDetailsSectionComponent", () => {
const cardView = new CardView();
cardView.expYear = "2022";
expect(patchCipherSpy).toHaveBeenCalledWith({
card: cardView,
});
expect(patchCipherSpy).toHaveBeenCalled();
const patchFn = patchCipherSpy.mock.lastCall[0];
const updatedCipher = patchFn(new CipherView());
expect(updatedCipher.card).toEqual(cardView);
});
it('disables `cardDetailsForm` when "disabled" is true', () => {

View File

@@ -90,9 +90,6 @@ export class CardDetailsSectionComponent implements OnInit {
{ name: "12 - " + this.i18nService.t("december"), value: "12" },
];
/** Local CardView, either created empty or set to the existing card instance */
private cardView: CardView;
constructor(
private cipherFormContainer: CipherFormContainer,
private formBuilder: FormBuilder,
@@ -103,21 +100,21 @@ export class CardDetailsSectionComponent implements OnInit {
this.cardDetailsForm.valueChanges
.pipe(takeUntilDestroyed())
.subscribe(({ cardholderName, number, brand, expMonth, expYear, code }) => {
// The input[type="number"] is returning a number, convert it to a string
// An empty field returns null, avoid casting `"null"` to a string
const expirationYear = expYear !== null ? `${expYear}` : null;
this.cipherFormContainer.patchCipher((cipher) => {
// The input[type="number"] is returning a number, convert it to a string
// An empty field returns null, avoid casting `"null"` to a string
const expirationYear = expYear !== null ? `${expYear}` : null;
const patchedCard = Object.assign(this.cardView, {
cardholderName,
number,
brand,
expMonth,
expYear: expirationYear,
code,
});
Object.assign(cipher.card, {
cardholderName,
number,
brand,
expMonth,
expYear: expirationYear,
code,
});
this.cipherFormContainer.patchCipher({
card: patchedCard,
return cipher;
});
});
@@ -133,9 +130,6 @@ export class CardDetailsSectionComponent implements OnInit {
}
ngOnInit() {
// If the original cipher has a card, use it. Otherwise, create a new card instance
this.cardView = this.originalCipherView?.card ?? new CardView();
if (this.originalCipherView?.card) {
this.setInitialValues();
}