-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(components/lookup): add test harnesses to country field (#2822)
- Loading branch information
1 parent
be34b8d
commit a5df333
Showing
12 changed files
with
536 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
1 change: 1 addition & 0 deletions
1
apps/code-examples/src/app/code-examples/lookup/country-field/basic/demo.component.html
This file contains 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
56 changes: 56 additions & 0 deletions
56
apps/code-examples/src/app/code-examples/lookup/country-field/basic/demo.component.spec.ts
This file contains 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,56 @@ | ||
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed'; | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { SkyInputBoxHarness } from '@skyux/forms/testing'; | ||
import { SkyCountryFieldHarness } from '@skyux/lookup/testing'; | ||
|
||
import { DemoComponent } from './demo.component'; | ||
|
||
describe('Basic country field demo', () => { | ||
async function setupTest(options: { dataSkyId: string }): Promise<{ | ||
harness: SkyCountryFieldHarness; | ||
fixture: ComponentFixture<DemoComponent>; | ||
}> { | ||
const fixture = TestBed.createComponent(DemoComponent); | ||
const loader = TestbedHarnessEnvironment.loader(fixture); | ||
|
||
const harness = await ( | ||
await loader.getHarness( | ||
SkyInputBoxHarness.with({ dataSkyId: options.dataSkyId }), | ||
) | ||
).queryHarness(SkyCountryFieldHarness); | ||
|
||
fixture.detectChanges(); | ||
await fixture.whenStable(); | ||
|
||
return { harness, fixture }; | ||
} | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [DemoComponent], | ||
}); | ||
}); | ||
|
||
it('should set up country field input', async () => { | ||
const { harness, fixture } = await setupTest({ | ||
dataSkyId: 'country-field', | ||
}); | ||
|
||
await harness.focus(); | ||
await harness.enterText('ger'); | ||
|
||
const searchResultsText = await harness.getSearchResultsText(); | ||
|
||
expect(searchResultsText.length).toBe(4); | ||
|
||
await harness.clear(); | ||
await harness.enterText('can'); | ||
|
||
const searchResults = await harness.getSearchResults(); | ||
await expectAsync(searchResults[1].getText()).toBeResolvedTo('Canada'); | ||
|
||
await searchResults[1].select(); | ||
const value = fixture.componentInstance.countryForm.get('country')?.value; | ||
expect(value?.name).toBe('Canada'); | ||
}); | ||
}); |
This file contains 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
7 changes: 7 additions & 0 deletions
7
libs/components/lookup/testing/src/country-field/country-field-harness-filters.ts
This file contains 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,7 @@ | ||
import { SkyHarnessFilters } from '@skyux/core/testing'; | ||
|
||
/** | ||
* A set of criteria that can be used to filter a list of `SkyCountryFieldHarness` instances. | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-empty-interface, @typescript-eslint/no-empty-object-type | ||
export interface SkyCountryFieldHarnessFilters extends SkyHarnessFilters {} |
183 changes: 183 additions & 0 deletions
183
libs/components/lookup/testing/src/country-field/country-field-harness.spec.ts
This file contains 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,183 @@ | ||
import { HarnessLoader } from '@angular/cdk/testing'; | ||
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed'; | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { SkyCountryFieldHarness } from './country-field-harness'; | ||
import { CountryFieldHarnessTestComponent } from './fixtures/country-field-harness-test.component'; | ||
|
||
describe('Country field harness', () => { | ||
async function setupTest(options: { dataSkyId?: string } = {}): Promise<{ | ||
countryFieldHarness: SkyCountryFieldHarness; | ||
fixture: ComponentFixture<CountryFieldHarnessTestComponent>; | ||
loader: HarnessLoader; | ||
}> { | ||
await TestBed.configureTestingModule({ | ||
imports: [CountryFieldHarnessTestComponent], | ||
}).compileComponents(); | ||
|
||
const fixture = TestBed.createComponent(CountryFieldHarnessTestComponent); | ||
const loader = TestbedHarnessEnvironment.loader(fixture); | ||
|
||
let countryFieldHarness: SkyCountryFieldHarness | undefined; | ||
if (options.dataSkyId) { | ||
countryFieldHarness = await loader.getHarness( | ||
SkyCountryFieldHarness.with({ dataSkyId: options.dataSkyId }), | ||
); | ||
} | ||
|
||
return { countryFieldHarness, fixture, loader }; | ||
} | ||
|
||
it('should focus and blur input', async () => { | ||
const { countryFieldHarness } = await setupTest({ | ||
dataSkyId: 'country-field', | ||
}); | ||
|
||
await expectAsync(countryFieldHarness?.isFocused()).toBeResolvedTo(false); | ||
|
||
await countryFieldHarness?.focus(); | ||
await expectAsync(countryFieldHarness?.isFocused()).toBeResolvedTo(true); | ||
|
||
await countryFieldHarness?.blur(); | ||
await expectAsync(countryFieldHarness?.isFocused()).toBeResolvedTo(false); | ||
}); | ||
|
||
it('should check if country field is disabled', async () => { | ||
const { fixture, countryFieldHarness } = await setupTest({ | ||
dataSkyId: 'country-field', | ||
}); | ||
|
||
await expectAsync(countryFieldHarness?.isDisabled()).toBeResolvedTo(false); | ||
|
||
fixture.componentInstance.disableForm(); | ||
|
||
await expectAsync(countryFieldHarness?.isDisabled()).toBeResolvedTo(true); | ||
}); | ||
|
||
it('should check if country field is open', async () => { | ||
const { countryFieldHarness } = await setupTest({ | ||
dataSkyId: 'country-field', | ||
}); | ||
|
||
await countryFieldHarness?.enterText('gr'); | ||
|
||
await expectAsync(countryFieldHarness?.isOpen()).toBeResolvedTo(true); | ||
}); | ||
|
||
it('should return search result harnesses', async () => { | ||
const { countryFieldHarness } = await setupTest({ | ||
dataSkyId: 'country-field', | ||
}); | ||
|
||
await countryFieldHarness?.enterText('gr'); | ||
|
||
const results = (await countryFieldHarness?.getSearchResults()) ?? []; | ||
|
||
await expectAsync(results[0].getDescriptorValue()).toBeResolvedTo('Greece'); | ||
await expectAsync(results[0].getText()).toBeResolvedTo('Greece'); | ||
}); | ||
|
||
it('should return search results text content', async () => { | ||
const { countryFieldHarness } = await setupTest({ | ||
dataSkyId: 'country-field', | ||
}); | ||
|
||
await countryFieldHarness?.enterText('gr'); | ||
|
||
await expectAsync( | ||
countryFieldHarness?.getSearchResultsText(), | ||
).toBeResolvedTo([ | ||
'Greece', | ||
'Greenland', | ||
'Grenada', | ||
'Montenegro', | ||
'St. Vincent & Grenadines', | ||
]); | ||
}); | ||
|
||
it('should select a search result', async () => { | ||
const { countryFieldHarness } = await setupTest({ | ||
dataSkyId: 'country-field', | ||
}); | ||
|
||
await countryFieldHarness?.enterText('gr'); | ||
const result = ((await countryFieldHarness?.getSearchResults()) ?? [])[0]; | ||
await result.select(); | ||
|
||
await expectAsync(countryFieldHarness?.getValue()).toBeResolvedTo('Greece'); | ||
}); | ||
|
||
it('should select a search result using filters', async () => { | ||
const { countryFieldHarness } = await setupTest({ | ||
dataSkyId: 'country-field', | ||
}); | ||
|
||
await countryFieldHarness?.enterText('gr'); | ||
await countryFieldHarness?.selectSearchResult({ | ||
text: 'Grenada', | ||
}); | ||
|
||
await expectAsync(countryFieldHarness?.getValue()).toBeResolvedTo( | ||
'Grenada', | ||
); | ||
}); | ||
|
||
it('should clear the input value', async () => { | ||
const { countryFieldHarness } = await setupTest({ | ||
dataSkyId: 'country-field', | ||
}); | ||
|
||
// First, set a value on the countryField. | ||
await countryFieldHarness?.enterText('gr'); | ||
await countryFieldHarness?.selectSearchResult({ | ||
text: 'Greenland', | ||
}); | ||
await expectAsync(countryFieldHarness?.getValue()).toBeResolvedTo( | ||
'Greenland', | ||
); | ||
|
||
// Now, clear the value. | ||
await countryFieldHarness?.clear(); | ||
await expectAsync(countryFieldHarness?.getValue()).toBeResolvedTo(''); | ||
}); | ||
|
||
it('should throw error if getting search results when country field not open', async () => { | ||
const { countryFieldHarness } = await setupTest({ | ||
dataSkyId: 'country-field', | ||
}); | ||
|
||
await expectAsync( | ||
countryFieldHarness?.getSearchResults(), | ||
).toBeRejectedWithError( | ||
'Unable to retrieve search results. The country field is closed.', | ||
); | ||
}); | ||
|
||
it('should throw error if filtered search results are empty', async () => { | ||
const { countryFieldHarness } = await setupTest({ | ||
dataSkyId: 'country-field', | ||
}); | ||
|
||
await countryFieldHarness?.enterText('gr'); | ||
|
||
await expectAsync( | ||
countryFieldHarness?.getSearchResults({ | ||
text: /invalidSearchText/, | ||
}), | ||
).toBeRejectedWithError( | ||
'Could not find search results matching filter(s): {"text":"/invalidSearchText/"}', | ||
); | ||
}); | ||
|
||
it('should return an empty array if search results are not filtered', async () => { | ||
const { countryFieldHarness } = await setupTest({ | ||
dataSkyId: 'country-field', | ||
}); | ||
|
||
await countryFieldHarness?.enterText('invalidSearchText'); | ||
|
||
await expectAsync(countryFieldHarness?.getSearchResults()).toBeResolvedTo( | ||
[], | ||
); | ||
}); | ||
}); |
Oops, something went wrong.