1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-16 16:23:44 +00:00

[PM-8027] Working through jest tests for the InlineMenuFieldQualificationService

This commit is contained in:
Cesar Gonzalez
2024-06-04 08:54:33 -05:00
parent 6199904d73
commit 3397261d55
2 changed files with 194 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
import { mock, MockProxy } from "jest-mock-extended"; import { mock, MockProxy } from "jest-mock-extended";
import AutofillField from "../models/autofill-field"; import AutofillField from "../models/autofill-field";
import AutofillForm from "../models/autofill-form";
import AutofillPageDetails from "../models/autofill-page-details"; import AutofillPageDetails from "../models/autofill-page-details";
import { AutoFillConstants } from "./autofill-constants"; import { AutoFillConstants } from "./autofill-constants";
@@ -19,7 +20,7 @@ describe("InlineMenuFieldQualificationService", () => {
}); });
describe("isFieldForLoginForm", () => { describe("isFieldForLoginForm", () => {
describe("validating a password field for a login form", () => { describe("qualifying a password field for a login form", () => {
describe("an invalid password field", () => { describe("an invalid password field", () => {
it("has a `new-password` autoCompleteType", () => { it("has a `new-password` autoCompleteType", () => {
const field = mock<AutofillField>({ const field = mock<AutofillField>({
@@ -97,6 +98,108 @@ describe("InlineMenuFieldQualificationService", () => {
false, false,
); );
}); });
describe("does not have a parent form element", () => {
beforeEach(() => {
pageDetails.forms = {};
});
it("on a page that has more than one password field", () => {
const field = mock<AutofillField>({
type: "password",
htmlID: "user-password",
htmlName: "user-password",
placeholder: "user-password",
form: "",
});
const secondField = mock<AutofillField>({
type: "password",
htmlID: "some-other-password",
htmlName: "some-other-password",
placeholder: "some-other-password",
});
pageDetails.fields = [field, secondField];
expect(
inlineMenuFieldQualificationService.isFieldForLoginForm(field, pageDetails),
).toBe(false);
});
it("on a page that has more than one visible username field", () => {
const field = mock<AutofillField>({
type: "password",
htmlID: "user-password",
htmlName: "user-password",
placeholder: "user-password",
form: "",
});
const usernameField = mock<AutofillField>({
type: "text",
htmlID: "user-username",
htmlName: "user-username",
placeholder: "user-username",
});
const secondUsernameField = mock<AutofillField>({
type: "text",
htmlID: "some-other-user-username",
htmlName: "some-other-user-username",
placeholder: "some-other-user-username",
});
pageDetails.fields = [field, usernameField, secondUsernameField];
expect(
inlineMenuFieldQualificationService.isFieldForLoginForm(field, pageDetails),
).toBe(false);
});
it("has a disabled `autocompleteType` value", () => {
const field = mock<AutofillField>({
type: "password",
htmlID: "user-password",
htmlName: "user-password",
placeholder: "user-password",
form: "",
autoCompleteType: "off",
});
expect(
inlineMenuFieldQualificationService.isFieldForLoginForm(field, pageDetails),
).toBe(false);
});
});
describe("has a parent form element", () => {
let form: MockProxy<AutofillForm>;
beforeEach(() => {
form = mock<AutofillForm>({ opid: "validFormId" });
pageDetails.forms = {
validFormId: form,
};
});
it("is structured with other password fields in the same form", () => {
const field = mock<AutofillField>({
type: "password",
htmlID: "user-password",
htmlName: "user-password",
placeholder: "user-password",
form: "validFormId",
});
const secondField = mock<AutofillField>({
type: "password",
htmlID: "some-other-password",
htmlName: "some-other-password",
placeholder: "some-other-password",
form: "validFormId",
});
pageDetails.fields = [field, secondField];
expect(
inlineMenuFieldQualificationService.isFieldForLoginForm(field, pageDetails),
).toBe(false);
});
});
}); });
describe("a valid password field", () => { describe("a valid password field", () => {
@@ -126,6 +229,94 @@ describe("InlineMenuFieldQualificationService", () => {
true, true,
); );
}); });
describe("does not have a parent form element", () => {
it("is the only password field on the page, has one username field on the page, and has a non-disabled `autocompleteType` value", () => {
pageDetails.forms = {};
const field = mock<AutofillField>({
type: "password",
htmlID: "user-password",
htmlName: "user-password",
placeholder: "user-password",
form: "",
autoCompleteType: "current-password",
});
const usernameField = mock<AutofillField>({
type: "text",
htmlID: "user-username",
htmlName: "user-username",
placeholder: "user-username",
});
pageDetails.fields = [field, usernameField];
expect(
inlineMenuFieldQualificationService.isFieldForLoginForm(field, pageDetails),
).toBe(true);
});
});
describe("has a parent form element", () => {
let form: MockProxy<AutofillForm>;
beforeEach(() => {
form = mock<AutofillForm>({ opid: "validFormId" });
pageDetails.forms = {
validFormId: form,
};
});
it("is the only password field within the form and has a visible username field", () => {
const field = mock<AutofillField>({
type: "password",
htmlID: "user-password",
htmlName: "user-password",
placeholder: "user-password",
form: "validFormId",
});
const secondPasswordField = mock<AutofillField>({
type: "password",
htmlID: "some-other-password",
htmlName: "some-other-password",
placeholder: "some-other-password",
form: "anotherFormId",
});
const usernameField = mock<AutofillField>({
type: "text",
htmlID: "user-username",
htmlName: "user-username",
placeholder: "user-username",
form: "validFormId",
});
pageDetails.fields = [field, secondPasswordField, usernameField];
expect(
inlineMenuFieldQualificationService.isFieldForLoginForm(field, pageDetails),
).toBe(true);
});
it("is the only password field within the form and has a non-disabled `autocompleteType` value", () => {
const field = mock<AutofillField>({
type: "password",
htmlID: "user-password",
htmlName: "user-password",
placeholder: "user-password",
form: "validFormId",
autoCompleteType: "",
});
const secondPasswordField = mock<AutofillField>({
type: "password",
htmlID: "some-other-password",
htmlName: "some-other-password",
placeholder: "some-other-password",
form: "anotherFormId",
});
pageDetails.fields = [field, secondPasswordField];
expect(
inlineMenuFieldQualificationService.isFieldForLoginForm(field, pageDetails),
).toBe(true);
});
});
}); });
}); });

View File

@@ -102,7 +102,8 @@ export class InlineMenuFieldQualificationService
return true; return true;
} }
// If the field has a form parent a no username field exists and the field has an autocomplete attribute set to "off" or "false", this is not a password field // If the field has a form parent a no username field exists and the field has an
// autocomplete attribute set to "off" or "false", this is not a password field
return !this.autocompleteDisabledValues.has(field.autoCompleteType); return !this.autocompleteDisabledValues.has(field.autoCompleteType);
} }