@@ -211,6 +211,41 @@ describe('DocViewer', () => {
211
211
expect ( clipboardSpy . copy ) . toHaveBeenCalled ( ) ;
212
212
} ) ;
213
213
214
+ it ( 'should show copy icon button for code blocks' , ( ) => {
215
+ const fixture = TestBed . createComponent ( DocViewerTestComponent ) ;
216
+ fixture . componentInstance . documentUrl = `http://material.angular.io/doc-with-code-block.html` ;
217
+ fixture . detectChanges ( ) ;
218
+
219
+ const url = fixture . componentInstance . documentUrl ;
220
+ http . expectOne ( url ) . flush ( FAKE_DOCS [ url ] ) ;
221
+
222
+ const docViewer = fixture . debugElement . query ( By . directive ( DocViewer ) ) ;
223
+ expect ( docViewer ) . not . toBeNull ( ) ;
224
+
225
+ // Query all copy buttons within code blocks
226
+ const iconButtons = fixture . debugElement . queryAll ( By . directive ( MatIconButton ) ) ;
227
+ // At least one icon button for copying code should exist
228
+ expect ( iconButtons . length ) . toBeGreaterThan ( 0 ) ;
229
+
230
+ // Click on the first icon button to trigger copying the code
231
+ iconButtons [ 0 ] . nativeNode . dispatchEvent ( new MouseEvent ( 'click' ) ) ;
232
+ fixture . detectChanges ( ) ;
233
+ expect ( clipboardSpy . copy ) . toHaveBeenCalledWith ( 'const example = "test code";' ) ;
234
+ } ) ;
235
+
236
+ it ( 'should create copy buttons for multiple code blocks' , ( ) => {
237
+ const fixture = TestBed . createComponent ( DocViewerTestComponent ) ;
238
+ fixture . componentInstance . documentUrl = `http://material.angular.io/doc-with-multiple-code-blocks.html` ;
239
+ fixture . detectChanges ( ) ;
240
+
241
+ const url = fixture . componentInstance . documentUrl ;
242
+ http . expectOne ( url ) . flush ( FAKE_DOCS [ url ] ) ;
243
+
244
+ const iconButtons = fixture . debugElement . queryAll ( By . directive ( MatIconButton ) ) ;
245
+ // Should have 3 copy buttons for 3 code blocks
246
+ expect ( iconButtons . length ) . toBe ( 3 ) ;
247
+ } ) ;
248
+
214
249
// TODO(mmalerba): Add test that example-viewer is instantiated.
215
250
} ) ;
216
251
@@ -262,6 +297,10 @@ const FAKE_DOCS: {[key: string]: string} = {
262
297
data-docs-api-module-import-button="import {MatIconModule} from '@angular/material/icon';">
263
298
</div>
264
299
</div>` ,
300
+ 'http://material.angular.io/doc-with-code-block.html' : `
301
+ <div class="docs-markdown">
302
+ <pre><code>const example = "test code";</code></pre>
303
+ </div>` ,
265
304
} ;
266
305
267
306
@Component ( {
0 commit comments