@@ -78,24 +78,84 @@ describe('service', function () {
7878 } ) ;
7979
8080 describe ( 'init' , function ( ) {
81- it ( 'embeds all components found in the DOM' , function ( ) {
82- // Arrange
83- const elements = [
84- '<div id="reportContainer1" powerbi-embed-url="https://embedded.powerbi.com/appTokenReportEmbed?reportId=ABC123" powerbi-type="report"></div>' ,
85- '<div id="reportContainer2" powerbi-embed-url="https://embedded.powerbi.com/appTokenReportEmbed?reportId=XYZ456" powerbi-type="report"></div>' ,
86- ] ;
81+ describe ( 'embeds all components found in the DOM' , function ( ) {
82+ let powerbi : service . Service ; // redefined for this scope
8783
88- elements . forEach ( element => {
89- $ ( element ) . appendTo ( '#powerbi-fixture' ) ;
84+ beforeEach ( function ( ) {
85+ powerbi = new service . Service ( factories . hpmFactory , factories . wpmpFactory , factories . routerFactory ) ;
86+ powerbi . accessToken = 'ABC123' ;
9087 } ) ;
9188
92- // Act
93- powerbi . init ( ) ;
89+ afterEach ( function ( ) {
90+ powerbi . wpmp . stop ( ) ;
91+ powerbi = null ;
92+ } ) ;
9493
95- // Assert
96- // If embed element has iframe inside it, assume embed action occurred
97- const iframes = document . querySelectorAll ( '[powerbi-embed-url] iframe' ) ;
98- expect ( iframes . length ) . toEqual ( 2 ) ;
94+ it ( 'should work with multiple elements without id' , function ( ) {
95+ // Arrange
96+ const elements = [
97+ '<div powerbi-embed-url="https://embedded.powerbi.com/appTokenReportEmbed?reportId=ABC123" powerbi-type="report"></div>' ,
98+ '<div powerbi-embed-url="https://embedded.powerbi.com/appTokenReportEmbed?reportId=XYZ456" powerbi-type="report"></div>' ,
99+ ] ;
100+
101+ elements . forEach ( element => {
102+ $ ( element ) . appendTo ( '#powerbi-fixture' ) ;
103+ } ) ;
104+
105+ // Act
106+ powerbi . init ( ) ;
107+
108+ // Assert
109+ // If embed element has iframe inside it, assume embed action occurred
110+ const iframes = document . querySelectorAll ( '[powerbi-embed-url] iframe' ) ;
111+ expect ( iframes . length ) . toEqual ( 2 ) ;
112+ // Check the number of components controlled by powerbi
113+ expect ( powerbi . getNumberOfComponents ( ) ) . toEqual ( 2 ) ;
114+ } ) ;
115+
116+ it ( 'should work with multiple elements with id' , function ( ) {
117+ // Arrange
118+ const elements = [
119+ '<div id="first-report" powerbi-embed-url="https://embedded.powerbi.com/appTokenReportEmbed?reportId=ABC123" powerbi-type="report"></div>' ,
120+ '<div id="second-report" powerbi-embed-url="https://embedded.powerbi.com/appTokenReportEmbed?reportId=XYZ456" powerbi-type="report"></div>' ,
121+ ] ;
122+
123+ elements . forEach ( element => {
124+ $ ( element ) . appendTo ( '#powerbi-fixture' ) ;
125+ } ) ;
126+
127+ // Act
128+ powerbi . init ( ) ;
129+
130+ // Assert
131+ // If embed element has iframe inside it, assume embed action occurred
132+ const iframes = document . querySelectorAll ( '[powerbi-embed-url] iframe' ) ;
133+ expect ( iframes . length ) . toEqual ( 2 ) ;
134+ // Check the number of components controlled by powerbi
135+ expect ( powerbi . getNumberOfComponents ( ) ) . toEqual ( 2 ) ;
136+ } ) ;
137+
138+ it ( 'should work with multiple elements with duplicated id' , function ( ) {
139+ // Arrange
140+ const elements = [
141+ '<div id="report" powerbi-embed-url="https://embedded.powerbi.com/appTokenReportEmbed?reportId=ABC123" powerbi-type="report"></div>' ,
142+ '<div id="report" powerbi-embed-url="https://embedded.powerbi.com/appTokenReportEmbed?reportId=XYZ456" powerbi-type="report"></div>' ,
143+ ] ;
144+
145+ elements . forEach ( element => {
146+ $ ( element ) . appendTo ( '#powerbi-fixture' ) ;
147+ } ) ;
148+
149+ // Act
150+ powerbi . init ( ) ;
151+
152+ // Assert
153+ // If embed element has iframe inside it, assume embed action occurred
154+ const iframes = document . querySelectorAll ( '[powerbi-embed-url] iframe' ) ;
155+ expect ( iframes . length ) . toEqual ( 2 ) ;
156+ // Check the number of components controlled by powerbi
157+ expect ( powerbi . getNumberOfComponents ( ) ) . toEqual ( 2 ) ;
158+ } ) ;
99159 } ) ;
100160
101161 it ( 'embeds all components found in the DOM without id attribute' , function ( ) {
0 commit comments