1
- import { HttpTestingController } from '@angular/common/http/testing' ;
2
1
import { NgModule } from '@angular/core' ;
3
- import { waitForAsync , ComponentFixture , inject , TestBed } from '@angular/core/testing' ;
2
+ import { ComponentFixture , TestBed } from '@angular/core/testing' ;
4
3
import { FormsModule , ReactiveFormsModule } from '@angular/forms' ;
5
4
import { MatAutocompleteModule } from '@angular/material/autocomplete' ;
6
5
import { MatInputModule } from '@angular/material/input' ;
7
6
import { MatSlideToggleModule } from '@angular/material/slide-toggle' ;
8
- import { MatSnackBar } from '@angular/material/snack-bar' ;
9
- import { Clipboard } from '@angular/cdk/clipboard' ;
10
7
11
8
import { EXAMPLE_COMPONENTS } from '@angular/components-examples' ;
12
- import { By } from '@angular/platform-browser' ;
13
9
import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
14
10
import { DocsAppTestingModule } from '../../testing/testing-module' ;
15
11
import { DocViewerModule } from '../doc-viewer/doc-viewer-module' ;
@@ -25,18 +21,24 @@ const exampleBasePath = `/docs-content/examples-highlighted/material/autocomplet
25
21
describe ( 'ExampleViewer' , ( ) => {
26
22
let fixture : ComponentFixture < ExampleViewer > ;
27
23
let component : ExampleViewer ;
28
- let http : HttpTestingController ;
29
24
let loader : HarnessLoader ;
30
25
31
- beforeEach ( waitForAsync ( ( ) => {
26
+ function waitForTabsLoad ( ) {
27
+ return new Promise < void > ( resolve => {
28
+ const interval = setInterval ( ( ) => {
29
+ if ( component . exampleTabs ) {
30
+ clearInterval ( interval ) ;
31
+ resolve ( ) ;
32
+ }
33
+ } , 100 ) ;
34
+ } ) ;
35
+ }
36
+
37
+ beforeEach ( ( ) => {
32
38
TestBed . configureTestingModule ( {
33
39
imports : [ DocViewerModule , DocsAppTestingModule , ReactiveFormsModule , TestExampleModule ] ,
34
40
} ) . compileComponents ( ) ;
35
- } ) ) ;
36
-
37
- beforeEach ( inject ( [ HttpTestingController ] , ( h : HttpTestingController ) => {
38
- http = h ;
39
- } ) ) ;
41
+ } ) ;
40
42
41
43
beforeEach ( ( ) => {
42
44
fixture = TestBed . createComponent ( ExampleViewer ) ;
@@ -45,7 +47,7 @@ describe('ExampleViewer', () => {
45
47
loader = TestbedHarnessEnvironment . loader ( fixture ) ;
46
48
} ) ;
47
49
48
- it ( 'should toggle between the 3 views' , waitForAsync ( ( ) => {
50
+ it ( 'should toggle between the 3 views' , ( ) => {
49
51
// need to specify a file because toggling from snippet to full changes the tabs to match
50
52
component . file = 'file.html' ;
51
53
component . view = 'snippet' ;
@@ -54,80 +56,89 @@ describe('ExampleViewer', () => {
54
56
expect ( component . view ) . toBe ( 'full' ) ;
55
57
component . toggleSourceView ( ) ;
56
58
expect ( component . view ) . toBe ( 'demo' ) ;
57
- } ) ) ;
59
+ } ) ;
58
60
59
- it ( 'should expand to HTML tab' , waitForAsync ( async ( ) => {
61
+ it ( 'should expand to HTML tab' , async ( ) => {
60
62
component . example = exampleKey ;
61
63
component . file = 'file.html' ;
62
64
component . view = 'snippet' ;
65
+ await waitForTabsLoad ( ) ;
63
66
component . toggleCompactView ( ) ;
64
67
65
68
const tabGroup = await loader . getHarness ( MatTabGroupHarness ) ;
66
69
const tab = await tabGroup . getSelectedTab ( ) ;
67
70
expect ( await tab . getLabel ( ) ) . toBe ( 'HTML' ) ;
68
- } ) ) ;
71
+ } ) ;
69
72
70
- it ( 'should expand to TS tab' , waitForAsync ( async ( ) => {
73
+ it ( 'should expand to TS tab' , async ( ) => {
71
74
component . example = exampleKey ;
72
75
component . file = EXAMPLE_COMPONENTS [ exampleKey ] . primaryFile ;
73
76
component . view = 'snippet' ;
77
+ await waitForTabsLoad ( ) ;
74
78
component . toggleCompactView ( ) ;
75
79
76
80
const tabGroup = await loader . getHarness ( MatTabGroupHarness ) ;
77
81
const tab = await tabGroup . getSelectedTab ( ) ;
78
82
expect ( await tab . getLabel ( ) ) . toBe ( 'TS' ) ;
79
- } ) ) ;
83
+ } ) ;
80
84
81
- it ( 'should expand to CSS tab' , waitForAsync ( async ( ) => {
85
+ it ( 'should expand to CSS tab' , async ( ) => {
82
86
component . example = exampleKey ;
83
87
component . file = 'file.css' ;
84
88
component . view = 'snippet' ;
89
+ await waitForTabsLoad ( ) ;
85
90
component . toggleCompactView ( ) ;
86
91
87
92
const tabGroup = await loader . getHarness ( MatTabGroupHarness ) ;
88
93
const tab = await tabGroup . getSelectedTab ( ) ;
89
94
expect ( await tab . getLabel ( ) ) . toBe ( 'CSS' ) ;
90
- } ) ) ;
95
+ } ) ;
91
96
92
- it ( 'should generate correct url with region' , waitForAsync ( ( ) => {
97
+ it ( 'should generate correct url with region' , async ( ) => {
93
98
component . example = exampleKey ;
94
99
component . region = 'region' ;
100
+ await waitForTabsLoad ( ) ;
95
101
const url = component . generateUrl ( 'a.b.html' ) ;
96
102
expect ( url ) . toBe ( exampleBasePath + '/a.b_region-html.html' ) ;
97
- } ) ) ;
103
+ } ) ;
98
104
99
- it ( 'should generate correct url without region' , waitForAsync ( ( ) => {
105
+ it ( 'should generate correct url without region' , async ( ) => {
100
106
component . example = exampleKey ;
101
107
component . region = undefined ;
108
+ await waitForTabsLoad ( ) ;
102
109
const url = component . generateUrl ( 'a.b.ts' ) ;
103
110
expect ( url ) . toBe ( exampleBasePath + '/a.b-ts.html' ) ;
104
- } ) ) ;
111
+ } ) ;
105
112
106
- it ( 'should print an error message about incorrect file type' , waitForAsync ( ( ) => {
113
+ it ( 'should print an error message about incorrect file type' , async ( ) => {
107
114
spyOn ( console , 'error' ) ;
108
115
component . example = exampleKey ;
109
116
component . file = 'file.bad' ;
117
+ await waitForTabsLoad ( ) ;
110
118
component . selectCorrectTab ( ) ;
111
119
112
120
expect ( console . error ) . toHaveBeenCalledWith ( `Could not find tab for file extension: "bad".` ) ;
113
- } ) ) ;
121
+ } ) ;
114
122
115
- it ( 'should set and return example properly' , waitForAsync ( ( ) => {
123
+ it ( 'should set and return example properly' , async ( ) => {
116
124
component . example = exampleKey ;
125
+ await waitForTabsLoad ( ) ;
117
126
const data = component . exampleData ;
118
127
expect ( data ) . toEqual ( EXAMPLE_COMPONENTS [ exampleKey ] ) ;
119
- } ) ) ;
128
+ } ) ;
120
129
121
- it ( 'should print an error message about missing example' , waitForAsync ( ( ) => {
130
+ it ( 'should print an error message about missing example' , async ( ) => {
122
131
spyOn ( console , 'error' ) ;
123
132
component . example = 'foobar' ;
133
+ await waitForTabsLoad ( ) ;
124
134
expect ( console . error ) . toHaveBeenCalled ( ) ;
125
135
expect ( console . error ) . toHaveBeenCalledWith ( 'Could not find example: foobar' ) ;
126
- } ) ) ;
136
+ } ) ;
127
137
128
- it ( 'should return docs-content path for example based on extension' , waitForAsync ( ( ) => {
138
+ it ( 'should return docs-content path for example based on extension' , async ( ) => {
129
139
// set example
130
140
component . example = exampleKey ;
141
+ await waitForTabsLoad ( ) ;
131
142
132
143
// get example file path for each extension
133
144
const extensions = [ 'ts' , 'css' , 'html' ] ;
@@ -138,16 +149,17 @@ describe('ExampleViewer', () => {
138
149
139
150
expect ( actual ) . toEqual ( expected ) ;
140
151
} ) ;
141
- } ) ) ;
152
+ } ) ;
142
153
143
154
describe ( 'view-source tab group' , ( ) => {
144
- it ( 'should only render HTML, TS and CSS files if no additional files are specified' , ( ) => {
155
+ it ( 'should only render HTML, TS and CSS files if no additional files are specified' , async ( ) => {
145
156
component . example = exampleKey ;
157
+ await waitForTabsLoad ( ) ;
146
158
147
159
expect ( component . _getExampleTabNames ( ) ) . toEqual ( [ 'HTML' , 'TS' , 'CSS' ] ) ;
148
160
} ) ;
149
161
150
- it ( 'should be able to render additional files' , ( ) => {
162
+ it ( 'should be able to render additional files' , async ( ) => {
151
163
EXAMPLE_COMPONENTS [ 'additional-files' ] = {
152
164
...EXAMPLE_COMPONENTS [ exampleKey ] ,
153
165
files : [
@@ -160,6 +172,7 @@ describe('ExampleViewer', () => {
160
172
} ;
161
173
162
174
component . example = 'additional-files' ;
175
+ await waitForTabsLoad ( ) ;
163
176
164
177
expect ( component . _getExampleTabNames ( ) ) . toEqual ( [
165
178
'HTML' ,
@@ -170,65 +183,18 @@ describe('ExampleViewer', () => {
170
183
] ) ;
171
184
} ) ;
172
185
173
- it ( 'should be possible for example to not have CSS or HTML files' , ( ) => {
186
+ it ( 'should be possible for example to not have CSS or HTML files' , async ( ) => {
174
187
EXAMPLE_COMPONENTS [ 'additional-files' ] = {
175
188
...EXAMPLE_COMPONENTS [ exampleKey ] ,
176
189
files : [ 'additional-files-example.ts' ] ,
177
190
} ;
178
191
179
192
component . example = 'additional-files' ;
193
+ await waitForTabsLoad ( ) ;
180
194
181
195
expect ( component . _getExampleTabNames ( ) ) . toEqual ( [ 'TS' ] ) ;
182
196
} ) ;
183
197
} ) ;
184
-
185
- describe ( 'copy button' , ( ) => {
186
- let button : HTMLElement ;
187
-
188
- beforeEach ( ( ) => {
189
- // Open source view
190
- component . example = exampleKey ;
191
- component . view = 'full' ;
192
- fixture . detectChanges ( ) ;
193
-
194
- for ( const url of Object . keys ( FAKE_DOCS ) ) {
195
- http . expectOne ( url ) . flush ( FAKE_DOCS [ url ] ) ;
196
- }
197
-
198
- // Select button element
199
- const btnDe = fixture . debugElement . query ( By . css ( '.docs-example-source-copy' ) ) ;
200
- button = btnDe ? btnDe . nativeElement : null ;
201
- } ) ;
202
-
203
- it ( 'should call clipboard service when clicked' , ( ) => {
204
- const clipboardService = TestBed . inject ( Clipboard ) ;
205
- const spy = spyOn ( clipboardService , 'copy' ) ;
206
- expect ( spy . calls . count ( ) ) . toBe ( 0 , 'before click' ) ;
207
- button . click ( ) ;
208
- expect ( spy . calls . count ( ) ) . toBe ( 1 , 'after click' ) ;
209
- expect ( spy . calls . argsFor ( 0 ) [ 0 ] ) . toBe ( 'my docs page' , 'click content' ) ;
210
- } ) ;
211
-
212
- it ( 'should display a message when copy succeeds' , ( ) => {
213
- const snackBar : MatSnackBar = TestBed . inject ( MatSnackBar ) ;
214
- const clipboardService = TestBed . inject ( Clipboard ) ;
215
- spyOn ( snackBar , 'open' ) ;
216
- spyOn ( clipboardService , 'copy' ) . and . returnValue ( true ) ;
217
- button . click ( ) ;
218
- expect ( snackBar . open ) . toHaveBeenCalledWith ( 'Code copied' , '' , { duration : 2500 } ) ;
219
- } ) ;
220
-
221
- it ( 'should display an error when copy fails' , ( ) => {
222
- const snackBar : MatSnackBar = TestBed . inject ( MatSnackBar ) ;
223
- const clipboardService = TestBed . inject ( Clipboard ) ;
224
- spyOn ( snackBar , 'open' ) ;
225
- spyOn ( clipboardService , 'copy' ) . and . returnValue ( false ) ;
226
- button . click ( ) ;
227
- expect ( snackBar . open ) . toHaveBeenCalledWith ( 'Copy failed. Please try again!' , '' , {
228
- duration : 2500 ,
229
- } ) ;
230
- } ) ;
231
- } ) ;
232
198
} ) ;
233
199
234
200
// Create a version of ExampleModule for testing with only one component so that we don't have
@@ -245,10 +211,3 @@ describe('ExampleViewer', () => {
245
211
] ,
246
212
} )
247
213
class TestExampleModule { }
248
-
249
- const FAKE_DOCS : { [ key : string ] : string } = {
250
- [ `${ exampleBasePath } /autocomplete-overview-example-html.html` ] : '<div>my docs page</div>' ,
251
- [ `${ exampleBasePath } /autocomplete-overview-example-ts.html` ] : '<span>const a = 1;</span>' ,
252
- [ `${ exampleBasePath } /autocomplete-overview-example-css.html` ] :
253
- '<pre>.class { color: black; }</pre>' ,
254
- } ;
0 commit comments