1+ import React from 'react' ;
2+ import { TechDocsAddonTester } from '@backstage/plugin-techdocs-addons-test-utils' ;
3+ import { SourcegraphNotebook } from '../plugin' ;
4+ import { SourcegraphNotebookCallbackParameters } from './SourcegraphNotebook' ;
5+
6+ const dom = (
7+ < body >
8+ < table className = 'language-text highlighttable' >
9+ < tbody >
10+ < tr >
11+ < td className = 'code' >
12+ < div className = 'language-text highlight' >
13+ < pre >
14+ < code > https://example.com/notebooks/foo</ code >
15+ </ pre >
16+ </ div >
17+ </ td >
18+ </ tr >
19+ </ tbody >
20+ </ table >
21+ < p > plugh</ p >
22+ < table className = 'language-text highlighttable' >
23+ < tbody >
24+ < tr >
25+ < td className = 'code' >
26+ < div className = 'language-text highlight' >
27+ < pre >
28+ < code > https://example.com/notebooks/bar</ code >
29+ </ pre >
30+ </ div >
31+ </ td >
32+ </ tr >
33+ </ tbody >
34+ </ table >
35+ < p > xyzzy</ p >
36+ < table className = 'highlighttable' >
37+ < tbody >
38+ < tr >
39+ < td className = 'code' >
40+ < div className = 'highlight' >
41+ < pre >
42+ < code > https://example.com/notebooks/baz</ code >
43+ </ pre >
44+ </ div >
45+ </ td >
46+ </ tr >
47+ </ tbody >
48+ </ table >
49+ < p > fred</ p >
50+ < table className = 'language-text highlighttable' >
51+ < tbody >
52+ < tr >
53+ < td className = 'code' >
54+ < div className = 'highlight' >
55+ < pre >
56+ < code > https://example.io/notebooks/gradle</ code >
57+ </ pre >
58+ </ div >
59+ </ td >
60+ </ tr >
61+ </ tbody >
62+ </ table >
63+ </ body >
64+ ) ;
65+
66+ describe ( 'SourcegraphNotebook' , ( ) => {
67+ it ( 'can render' , async ( ) => {
68+ const { shadowRoot } = await TechDocsAddonTester . buildAddonsInTechDocs ( [
69+ < SourcegraphNotebook domain = 'example.com' />
70+ ] )
71+ . withDom ( dom )
72+ . renderWithEffects ( ) ;
73+ const foo = shadowRoot ?. querySelector ( '[data-notebook-id="foo"]' ) as Element ;
74+ expect ( foo ) . not . toBeNull ( ) ;
75+ expect ( foo . querySelector ( 'iframe' ) ?. outerHTML )
76+ . toBe ( '<iframe src="https://example.com/embed/notebooks/foo" frameborder="0" sandbox="allow-scripts allow-same-origin allow-popups"></iframe>' ) ;
77+ const bar = shadowRoot ?. querySelector ( '[data-notebook-id="bar"]' ) as Element ;
78+ expect ( bar ) . not . toBeNull ( ) ;
79+ expect ( bar . querySelector ( 'iframe' ) ?. outerHTML )
80+ . toBe ( '<iframe src="https://example.com/embed/notebooks/bar" frameborder="0" sandbox="allow-scripts allow-same-origin allow-popups"></iframe>' ) ;
81+ expect ( shadowRoot ?. querySelector ( '[data-notebook-id="baz"]' ) ) . toBeNull ( ) ;
82+ expect ( shadowRoot ?. querySelector ( '[data-notebook-id="gradle"]' ) ) . toBeNull ( ) ;
83+ } ) ;
84+ it ( 'can render with callback' , async ( ) => {
85+ const args : Record < string , {
86+ container : HTMLDivElement ,
87+ iframe : HTMLIFrameElement ,
88+ id : string ,
89+ url : string
90+ } > = { } ;
91+ const callback = ( { container, iframe, id, url } : SourcegraphNotebookCallbackParameters ) => {
92+ args [ id ] = { container, iframe, id, url } ;
93+ } ;
94+ await TechDocsAddonTester . buildAddonsInTechDocs ( [
95+ < SourcegraphNotebook domain = 'example.com' callback = { callback } />
96+ ] )
97+ . withDom ( dom )
98+ . renderWithEffects ( ) ;
99+ expect ( args . foo . container ) . not . toBeNull ( ) ;
100+ expect ( args . foo . iframe ) . not . toBeNull ( ) ;
101+ expect ( args . foo . id ) . toBe ( 'foo' ) ;
102+ expect ( args . foo . url ) . toBe ( 'https://example.com/notebooks/foo' ) ;
103+ expect ( args . bar . container ) . not . toBeNull ( ) ;
104+ expect ( args . bar . iframe ) . not . toBeNull ( ) ;
105+ expect ( args . bar . id ) . toBe ( 'bar' ) ;
106+ expect ( args . bar . url ) . toBe ( 'https://example.com/notebooks/bar' ) ;
107+ } ) ;
108+ describe ( 'callback' , ( ) => {
109+ it ( 'does not auto-append iframe' , async ( ) => {
110+ const { shadowRoot } = await TechDocsAddonTester . buildAddonsInTechDocs ( [
111+ < SourcegraphNotebook domain = 'example.com' callback = { ( ) => { } } />
112+ ] )
113+ . withDom ( dom )
114+ . renderWithEffects ( ) ;
115+ const iframes = ( shadowRoot ?. querySelectorAll ( 'iframe' ) ?? [ ] ) as NodeListOf < HTMLIFrameElement > ;
116+ expect ( iframes . length ) . toBe ( 0 ) ;
117+ } ) ;
118+ it ( 'can append iframe' , async ( ) => {
119+ const callback = ( { container, iframe } : SourcegraphNotebookCallbackParameters ) => {
120+ container . append ( iframe ) ;
121+ } ;
122+ const { shadowRoot } = await TechDocsAddonTester . buildAddonsInTechDocs ( [
123+ < SourcegraphNotebook domain = 'example.com' callback = { callback } />
124+ ] )
125+ . withDom ( dom )
126+ . renderWithEffects ( ) ;
127+ const iframes = ( shadowRoot ?. querySelectorAll ( 'iframe' ) ?? [ ] ) as NodeListOf < HTMLIFrameElement > ;
128+ expect ( iframes . length ) . toBe ( 2 ) ;
129+ } ) ;
130+ } ) ;
131+ } ) ;
0 commit comments