1+ import { HarnessLoader } from '@angular/cdk-experimental/testing' ;
12import { ComponentFixture , TestBed } from '@angular/core/testing' ;
23import { TestbedHarnessEnvironment } from '../testbed' ;
34import { MainComponentHarness } from './harnesses/main-component-harness' ;
5+ import { SubComponentHarness } from './harnesses/sub-component-harness' ;
46import { TestComponentsModule } from './test-components-module' ;
57import { TestMainComponent } from './test-main-component' ;
68
7- describe ( 'Testbed Helper Test' , ( ) => {
8- let harness : MainComponentHarness ;
9+ describe ( 'TestbedHarnessEnvironment' , ( ) => {
910 let fixture : ComponentFixture < { } > ;
11+
1012 beforeEach ( async ( ) => {
1113 await TestBed . configureTestingModule ( { imports : [ TestComponentsModule ] } ) . compileComponents ( ) ;
1214 fixture = TestBed . createComponent ( TestMainComponent ) ;
13- harness = await TestbedHarnessEnvironment . harnessForFixtureRoot ( fixture , MainComponentHarness ) ;
1415 } ) ;
1516
16- describe ( 'Locator' , ( ) => {
17- it ( 'should be able to locate a element based on CSS selector' , async ( ) => {
17+ describe ( 'HarnessLoader' , ( ) => {
18+ let loader : HarnessLoader ;
19+
20+ beforeEach ( async ( ) => {
21+ loader = TestbedHarnessEnvironment . create ( fixture ) ;
22+ } ) ;
23+
24+ it ( 'should create HarnessLoader from fixture' , async ( ) => {
25+ expect ( loader ) . not . toBeNull ( ) ;
26+ } ) ;
27+
28+ it ( 'should create ComponentHarness for fixture' , async ( ) => {
29+ const harness =
30+ await TestbedHarnessEnvironment . harnessForFixtureRoot ( fixture , MainComponentHarness ) ;
31+ expect ( harness ) . not . toBeNull ( ) ;
32+ } ) ;
33+
34+ it ( 'should find required HarnessLoader for child element' , async ( ) => {
35+ const subcomponentsLoader = await loader . findRequired ( '.subcomponents' ) ;
36+ expect ( subcomponentsLoader ) . not . toBeNull ( ) ;
37+ } ) ;
38+
39+ it ( 'should error after failing to find required HarnessLoader for child element' , async ( ) => {
40+ try {
41+ await loader . findRequired ( 'error' ) ;
42+ fail ( 'Expected to throw' ) ;
43+ } catch ( e ) {
44+ expect ( e . message )
45+ . toBe ( 'Expected to find element matching selector: "error", but none was found' ) ;
46+ }
47+ } ) ;
48+
49+ it ( 'should find optional HarnessLoader for child element' , async ( ) => {
50+ const subcomponentsLoader = await loader . findOptional ( '.subcomponents' ) ;
51+ const nullLoader = await loader . findOptional ( 'wrong-selector' ) ;
52+ expect ( subcomponentsLoader ) . not . toBeNull ( ) ;
53+ expect ( nullLoader ) . toBeNull ( ) ;
54+ } ) ;
55+
56+ it ( 'should find all HarnessLoaders for child elements' , async ( ) => {
57+ const loaders = await loader . findAll ( '.subcomponents,.counters' ) ;
58+ expect ( loaders . length ) . toBe ( 2 ) ;
59+ } ) ;
60+
61+ it ( 'should get first matching component for required harness' , async ( ) => {
62+ const harness = await loader . requiredHarness ( SubComponentHarness ) ;
63+ expect ( harness ) . not . toBeNull ( ) ;
64+ expect ( await ( await harness . title ( ) ) . text ( ) ) . toBe ( 'List of test tools' ) ;
65+ } ) ;
66+
67+ it ( 'should throw if no matching component found for required harness' , async ( ) => {
68+ const countersLoader = await loader . findRequired ( '.counters' ) ;
69+ try {
70+ await countersLoader . requiredHarness ( SubComponentHarness ) ;
71+ fail ( 'Expected to throw' ) ;
72+ } catch ( e ) {
73+ expect ( e . message )
74+ . toBe ( 'Expected to find element matching selector: "test-sub", but none was found' ) ;
75+ }
76+ } ) ;
77+
78+ it ( 'should get first matching component for optional harness' , async ( ) => {
79+ const countersLoader = await loader . findRequired ( '.counters' ) ;
80+ const harness1 = await loader . optionalHarness ( SubComponentHarness ) ;
81+ const harness2 = await countersLoader . optionalHarness ( SubComponentHarness ) ;
82+ expect ( harness1 ) . not . toBeNull ( ) ;
83+ expect ( await ( await harness1 ! . title ( ) ) . text ( ) ) . toBe ( 'List of test tools' ) ;
84+ expect ( harness2 ) . toBeNull ( ) ;
85+ } ) ;
86+
87+ it ( 'should get all matching components for all harnesses' , async ( ) => {
88+ const harnesses = await loader . allHarnesses ( SubComponentHarness ) ;
89+ expect ( harnesses . length ) . toBe ( 2 ) ;
90+ } ) ;
91+ } ) ;
92+
93+ describe ( 'ComponentHarness' , ( ) => {
94+ let harness : MainComponentHarness ;
95+
96+ beforeEach ( async ( ) => {
97+ harness =
98+ await TestbedHarnessEnvironment . harnessForFixtureRoot ( fixture , MainComponentHarness ) ;
99+ } ) ;
100+
101+ it ( 'should locate a required element based on CSS selector' , async ( ) => {
18102 const title = await harness . title ( ) ;
19103 expect ( await title . text ( ) ) . toBe ( 'Main Component' ) ;
20104 } ) ;
21105
22- it ( 'should be able to locate all elements based on CSS selector' ,
23- async ( ) => {
24- const labels = await harness . allLabels ( ) ;
25- expect ( labels . length ) . toBe ( 2 ) ;
26- expect ( await labels [ 0 ] . text ( ) ) . toBe ( 'Count:' ) ;
27- expect ( await labels [ 1 ] . text ( ) ) . toBe ( 'AsyncCounter:' ) ;
28- } ) ;
106+ it ( 'should throw when failing to locate a required element based on CSS selector' , async ( ) => {
107+ try {
108+ await harness . errorItem ( ) ;
109+ fail ( 'Expected to throw' ) ;
110+ } catch ( e ) {
111+ expect ( e . message ) . toBe (
112+ 'Expected to find element matching selector: "wrong locator", but none was found' ) ;
113+ }
114+ } ) ;
115+
116+ it ( 'should locate an optional element based on CSS selector' , async ( ) => {
117+ const present = await harness . optionalDiv ( ) ;
118+ const missing = await harness . nullItem ( ) ;
119+ expect ( present ) . not . toBeNull ( ) ;
120+ expect ( await present ! . text ( ) ) . toBe ( 'Hello Yi from Angular 2!' ) ;
121+ expect ( missing ) . toBeNull ( ) ;
122+ } ) ;
29123
30- it ( 'should be able to locate the sub harnesses' , async ( ) => {
124+ it ( 'should locate all elements based on CSS selector' , async ( ) => {
125+ const labels = await harness . allLabels ( ) ;
126+ expect ( labels . length ) . toBe ( 2 ) ;
127+ expect ( await labels [ 0 ] . text ( ) ) . toBe ( 'Count:' ) ;
128+ expect ( await labels [ 1 ] . text ( ) ) . toBe ( 'AsyncCounter:' ) ;
129+ } ) ;
130+
131+ it ( 'should locate required sub harnesses' , async ( ) => {
31132 const items = await harness . getTestTools ( ) ;
32133 expect ( items . length ) . toBe ( 3 ) ;
33134 expect ( await items [ 0 ] . text ( ) ) . toBe ( 'Protractor' ) ;
34135 expect ( await items [ 1 ] . text ( ) ) . toBe ( 'TestBed' ) ;
35136 expect ( await items [ 2 ] . text ( ) ) . toBe ( 'Other' ) ;
36137 } ) ;
37138
38- it ( 'should be able to locate all sub harnesses' , async ( ) => {
139+ it ( 'should throw when failing to locate required sub harnesses' , async ( ) => {
140+ try {
141+ await harness . errorSubComponent ( ) ;
142+ fail ( 'Expected to throw' ) ;
143+ } catch ( e ) {
144+ expect ( e . message ) . toBe (
145+ 'Expected to find element matching selector: "wrong-selector", but none was found' ) ;
146+ }
147+ } ) ;
148+
149+ it ( 'should locate optional sub harnesses' , async ( ) => {
150+ const present = await harness . optionalSubComponent ( ) ;
151+ const missing = await harness . nullComponentHarness ( ) ;
152+ expect ( present ) . not . toBeNull ( ) ;
153+ expect ( await ( await present ! . title ( ) ) . text ( ) ) . toBe ( 'List of test tools' ) ;
154+ expect ( missing ) . toBeNull ( ) ;
155+ } ) ;
156+
157+ it ( 'should locate all sub harnesses' , async ( ) => {
39158 const alllists = await harness . allLists ( ) ;
40159 const items1 = await alllists [ 0 ] . getItems ( ) ;
41160 const items2 = await alllists [ 1 ] . getItems ( ) ;
@@ -49,9 +168,31 @@ describe('Testbed Helper Test', () => {
49168 expect ( await items2 [ 1 ] . text ( ) ) . toBe ( 'Integration Test' ) ;
50169 expect ( await items2 [ 2 ] . text ( ) ) . toBe ( 'Performance Test' ) ;
51170 } ) ;
171+
172+ it ( 'should wait for async opeartion to complete' , async ( ) => {
173+ const asyncCounter = await harness . asyncCounter ( ) ;
174+ expect ( await asyncCounter . text ( ) ) . toBe ( '5' ) ;
175+ await harness . increaseCounter ( 3 ) ;
176+ expect ( await asyncCounter . text ( ) ) . toBe ( '8' ) ;
177+ } ) ;
178+
179+ it ( 'can get elements outside of host' , async ( ) => {
180+ const subcomponents = await harness . allLists ( ) ;
181+ expect ( subcomponents [ 0 ] ) . not . toBeNull ( ) ;
182+ const globalEl = await subcomponents [ 0 ] ! . globalElement ( ) ;
183+ expect ( globalEl ) . not . toBeNull ( ) ;
184+ expect ( await globalEl . text ( ) ) . toBe ( 'Hello Yi from Angular 2!' ) ;
185+ } ) ;
52186 } ) ;
53187
54- describe ( 'Test element' , ( ) => {
188+ describe ( 'TestElement' , ( ) => {
189+ let harness : MainComponentHarness ;
190+
191+ beforeEach ( async ( ) => {
192+ harness =
193+ await TestbedHarnessEnvironment . harnessForFixtureRoot ( fixture , MainComponentHarness ) ;
194+ } ) ;
195+
55196 it ( 'should be able to clear' , async ( ) => {
56197 const input = await harness . input ( ) ;
57198 await input . sendKeys ( 'Yi' ) ;
@@ -80,8 +221,7 @@ describe('Testbed Helper Test', () => {
80221 it ( 'focuses the element before sending key' , async ( ) => {
81222 const input = await harness . input ( ) ;
82223 await input . sendKeys ( 'Yi' ) ;
83- expect ( await input . getAttribute ( 'id' ) )
84- . toBe ( document . activeElement ! . id ) ;
224+ expect ( await input . getAttribute ( 'id' ) ) . toBe ( document . activeElement ! . id ) ;
85225 } ) ;
86226
87227 it ( 'should be able to hover' , async ( ) => {
@@ -108,36 +248,4 @@ describe('Testbed Helper Test', () => {
108248 expect ( await title . getCssValue ( 'height' ) ) . toBe ( '50px' ) ;
109249 } ) ;
110250 } ) ;
111-
112- describe ( 'Async operation' , ( ) => {
113- it ( 'should wait for async opeartion to complete' , async ( ) => {
114- const asyncCounter = await harness . asyncCounter ( ) ;
115- expect ( await asyncCounter . text ( ) ) . toBe ( '5' ) ;
116- await harness . increaseCounter ( 3 ) ;
117- expect ( await asyncCounter . text ( ) ) . toBe ( '8' ) ;
118- } ) ;
119- } ) ;
120-
121- describe ( 'Allow null' , ( ) => {
122- it ( 'should allow element to be null when setting allowNull' , async ( ) => {
123- expect ( await harness . nullItem ( ) ) . toBe ( null ) ;
124- } ) ;
125-
126- it ( 'should allow harness to be null when setting allowNull' , async ( ) => {
127- expect ( await harness . nullComponentHarness ( ) ) . toBe ( null ) ;
128- } ) ;
129- } ) ;
130-
131- describe ( 'Throw error' , ( ) => {
132- it ( 'should show the correct error' , async ( ) => {
133- try {
134- await harness . errorItem ( ) ;
135- fail ( 'Should throw error' ) ;
136- } catch ( err ) {
137- expect ( err . message )
138- . toBe (
139- 'Expected to find element matching selector: "wrong locator", but none was found' ) ;
140- }
141- } ) ;
142- } ) ;
143251} ) ;
0 commit comments