-
Notifications
You must be signed in to change notification settings - Fork 32
Add specs for remaining under-covered form controls #650
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fpigeonjr
wants to merge
2
commits into
master
Choose a base branch
from
gh-631-form-control-specs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| import { TestBed } from "@angular/core/testing"; | ||
| import { By } from "@angular/platform-browser"; | ||
| import { FormsModule, FormControl } from "@angular/forms"; | ||
| import { SamCheckboxComponent } from "./checkbox.component"; | ||
| import { FieldsetWrapper } from "../../wrappers/fieldset-wrapper"; | ||
| import { SamFormService } from "../../form-service"; | ||
| import type { ComponentFixture } from "@angular/core/testing"; | ||
|
|
||
| describe("The Sam Checkbox component", () => { | ||
| describe("rendered tests", () => { | ||
| let component: SamCheckboxComponent; | ||
| let fixture: ComponentFixture<SamCheckboxComponent>; | ||
|
|
||
| const options = [ | ||
| { value: "dc", label: "Washington DC", name: "dc" }, | ||
| { value: "ma", label: "Maryland", name: "ma" }, | ||
| { value: "va", label: "Virginia", name: "va" }, | ||
| ]; | ||
|
|
||
| beforeEach(() => { | ||
| TestBed.configureTestingModule({ | ||
| imports: [FormsModule], | ||
| declarations: [SamCheckboxComponent, FieldsetWrapper], | ||
| providers: [SamFormService], | ||
| }); | ||
|
|
||
| fixture = TestBed.createComponent(SamCheckboxComponent); | ||
| component = fixture.componentInstance; | ||
| component.options = options; | ||
| component.label = "Pick a state"; | ||
| component.name = "my-checkboxes"; | ||
| }); | ||
|
|
||
| it("should implement controlvalueaccessor", () => { | ||
| component.registerOnChange(() => undefined); | ||
| component.registerOnTouched(() => undefined); | ||
| component.setDisabledState(true); | ||
| expect(component.disabled).toBe(true); | ||
| component.writeValue(["dc"]); | ||
| expect(component.value).toEqual(["dc"]); | ||
| }); | ||
|
|
||
| it("should display a checkbox for each option", () => { | ||
| fixture.detectChanges(); | ||
| const checkboxes = fixture.debugElement.queryAll(By.css("input")); | ||
| expect(checkboxes.length).toBe(options.length); | ||
| }); | ||
|
|
||
| it("should check the checkbox when the option value is in the model", () => { | ||
| component.writeValue(["ma"]); | ||
| fixture.detectChanges(); | ||
| expect(component.isChecked("ma")).toBe(true); | ||
| expect(component.isChecked("dc")).toBe(false); | ||
| }); | ||
|
|
||
| it("should insert a checked option into the model in options order", () => { | ||
| fixture.detectChanges(); | ||
| component.onCheckChanged("va", true, "va"); | ||
| component.onCheckChanged("dc", true, "dc"); | ||
| // dc appears before va in the options list, so it should be | ||
| // inserted before va even though it was checked second | ||
| expect(component.model).toEqual(["dc", "va"]); | ||
| }); | ||
|
|
||
| it("should remove an option from the model when unchecked", () => { | ||
| fixture.detectChanges(); | ||
| component.writeValue(["dc", "ma"]); | ||
| component.onCheckChanged("dc", false, "dc"); | ||
| expect(component.model).toEqual(["ma"]); | ||
| }); | ||
|
|
||
| it("should emit modelChange and optionSelected when an option changes", () => { | ||
| fixture.detectChanges(); | ||
| let emittedModel: unknown; | ||
| let emittedSelection: unknown; | ||
| component.modelChange.subscribe((val) => (emittedModel = val)); | ||
| component.optionSelected.subscribe((val) => (emittedSelection = val)); | ||
|
|
||
| component.onCheckChanged("dc", true, "dc"); | ||
|
|
||
| expect(emittedModel).toEqual(["dc"]); | ||
| expect(emittedSelection).toEqual({ | ||
| model: ["dc"], | ||
| selected: "dc", | ||
| id: "dc", | ||
| }); | ||
| }); | ||
|
|
||
| it("should select all options when the select-all checkbox is checked", () => { | ||
| component.hasSelectAll = true; | ||
| fixture.detectChanges(); | ||
| component.onSelectAllChange(true); | ||
| expect(component.model).toEqual(["dc", "ma", "va"]); | ||
| }); | ||
|
|
||
| it("should clear all options when the select-all checkbox is unchecked", () => { | ||
| component.hasSelectAll = true; | ||
| fixture.detectChanges(); | ||
| component.writeValue(["dc", "ma", "va"]); | ||
| component.onSelectAllChange(false); | ||
| expect(component.model).toEqual([]); | ||
| }); | ||
|
|
||
| it("should not count disabled options as active options", () => { | ||
| component.options = [ | ||
| { value: "dc", label: "Washington DC", name: "dc" }, | ||
| { value: "ma", label: "Maryland", name: "ma", disabled: true }, | ||
| ]; | ||
| fixture.detectChanges(); | ||
| expect(component.activeOptions).toBe(1); | ||
| }); | ||
|
|
||
| it("should not allow a disabled option to remain selected via setModelValue", () => { | ||
| component.options = [ | ||
| { value: "dc", label: "Washington DC", name: "dc" }, | ||
| { value: "ma", label: "Maryland", name: "ma", disabled: true }, | ||
| ]; | ||
| fixture.detectChanges(); | ||
| component.setModelValue(["dc", "ma"]); | ||
| expect(component.model).toEqual(["dc"]); | ||
| }); | ||
|
|
||
| it("should derive the select-all label from the id when set", () => { | ||
| component.id = "my-id"; | ||
| fixture.detectChanges(); | ||
| expect(component.checkAllLabelOrId()).toBe("all-my-id"); | ||
| }); | ||
|
|
||
| it("should derive the select-all label from the label when no id is set", () => { | ||
| component.id = undefined; | ||
| fixture.detectChanges(); | ||
| expect(component.checkAllLabelOrId()).toBe("all-Pick a state"); | ||
| }); | ||
|
|
||
| it("should format errors from a form control on init and on value changes", () => { | ||
| const control = new FormControl([]); | ||
| component.control = control; | ||
| fixture.detectChanges(); | ||
| component.ngOnInit(); | ||
|
|
||
| expect(() => control.setValue(["dc"])).not.toThrow(); | ||
| }); | ||
|
|
||
| it("should show a hint message", () => { | ||
| const hint = "Life pro tip: eat vegetables"; | ||
| component.hint = hint; | ||
| fixture.detectChanges(); | ||
| expect(fixture.nativeElement.innerHTML).toContain(hint); | ||
| }); | ||
|
|
||
| it("should show an error message", () => { | ||
| const errorMessage = "Uh-oh, something went wrong"; | ||
| component.errorMessage = errorMessage; | ||
| fixture.detectChanges(); | ||
| expect(fixture.nativeElement.innerHTML).toContain(errorMessage); | ||
| }); | ||
|
|
||
| it("should show a label", () => { | ||
| const labelText = "Pick from the following options"; | ||
| component.label = labelText; | ||
| fixture.detectChanges(); | ||
| expect(fixture.nativeElement.innerHTML).toContain(labelText); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.