11import { newSpecPage } from '@stencil/core/testing' ;
22import { PosSwitch } from './pos-switch' ;
33import { when } from 'jest-when' ;
4+ import { RdfType , Thing } from '@pod-os/core' ;
5+ import { Subject } from 'rxjs' ;
46
57describe ( 'pos-switch' , ( ) => {
68 it ( 'contains only templates initially' , async ( ) => {
@@ -35,8 +37,6 @@ describe('pos-switch', () => {
3537 </pos-case>
3638 </pos-switch>` ,
3739 } ) ;
38- await page . rootInstance . receiveResource ( { types : ( ) => [ ] } ) ;
39- await page . waitForChanges ( ) ;
4040 expect ( page . rootInstance . caseElements . length ) . toEqual ( 2 ) ;
4141 expect ( page . rootInstance . caseElements [ 0 ] . getAttribute ( 'if-typeof' ) ) . toEqual ( 'http://schema.org/Recipe' ) ;
4242 expect ( page . rootInstance . caseElements [ 1 ] . getAttribute ( 'if-typeof' ) ) . toEqual ( 'http://schema.org/Video' ) ;
@@ -54,8 +54,6 @@ describe('pos-switch', () => {
5454 </pos-case>
5555 </pos-switch>` ,
5656 } ) ;
57- await page . rootInstance . receiveResource ( { types : ( ) => [ ] } ) ;
58- await page . waitForChanges ( ) ;
5957 expect ( page . rootInstance . caseElements . length ) . toEqual ( 1 ) ;
6058 expect ( page . rootInstance . caseElements [ 0 ] . getAttribute ( 'if-typeof' ) ) . toEqual ( 'http://schema.org/Recipe' ) ;
6159 } ) ;
@@ -65,15 +63,11 @@ describe('pos-switch', () => {
6563 components : [ PosSwitch ] ,
6664 html : `<pos-switch></pos-switch>` ,
6765 } ) ;
68- await page . rootInstance . receiveResource ( { } ) ;
69- await page . waitForChanges ( ) ;
70-
7166 const el : HTMLElement = page . root as unknown as HTMLElement ;
72-
7367 expect ( el . textContent ) . toEqual ( 'No pos-case elements found' ) ;
7468 } ) ;
7569
76- it ( 'renders matching condition templates' , async ( ) => {
70+ it ( 'renders matching condition templates, reactively ' , async ( ) => {
7771 const page = await newSpecPage ( {
7872 components : [ PosSwitch ] ,
7973 html : `
@@ -95,14 +89,17 @@ describe('pos-switch', () => {
9589 </pos-case>
9690 </pos-switch>` ,
9791 } ) ;
98- await page . rootInstance . receiveResource ( {
99- types : ( ) => [
100- {
101- label : 'Recipe' ,
102- uri : 'http://schema.org/Recipe' ,
103- } ,
104- ] ,
105- } ) ;
92+ const observedTypes$ = new Subject < RdfType [ ] > ( ) ;
93+ const thing = {
94+ observeTypes : ( ) => observedTypes$ ,
95+ } as unknown as Thing ;
96+ page . rootInstance . receiveResource ( thing ) ;
97+ observedTypes$ . next ( [
98+ {
99+ label : 'Recipe' ,
100+ uri : 'http://schema.org/Recipe' ,
101+ } ,
102+ ] ) ;
106103 await page . waitForChanges ( ) ;
107104 expect ( page . root ?. innerHTML ) . toEqualHtml ( `
108105 <div>Recipe 1</div>
@@ -121,14 +118,17 @@ describe('pos-switch', () => {
121118 <pos-case else><template><div>No matches</div></template></pos-case>
122119 </pos-switch>` ,
123120 } ) ;
124- await page . rootInstance . receiveResource ( {
125- types : ( ) => [
126- {
127- label : 'Recipe' ,
128- uri : 'http://schema.org/Recipe' ,
129- } ,
130- ] ,
131- } ) ;
121+ const observedTypes$ = new Subject < RdfType [ ] > ( ) ;
122+ const thing = {
123+ observeTypes : ( ) => observedTypes$ ,
124+ } as unknown as Thing ;
125+ page . rootInstance . receiveResource ( thing ) ;
126+ observedTypes$ . next ( [
127+ {
128+ label : 'Recipe' ,
129+ uri : 'http://schema.org/Recipe' ,
130+ } ,
131+ ] ) ;
132132 await page . waitForChanges ( ) ;
133133 expect ( page . root ?. innerHTML ) . toEqualHtml ( `
134134 <div>Recipe 1</div>
@@ -144,14 +144,17 @@ describe('pos-switch', () => {
144144 <pos-case else><template><div>No matches</div></template></pos-case>
145145 </pos-switch>` ,
146146 } ) ;
147- await page . rootInstance . receiveResource ( {
148- types : ( ) => [
149- {
150- label : 'Recipe' ,
151- uri : 'http://schema.org/Recipe' ,
152- } ,
153- ] ,
154- } ) ;
147+ const observedTypes$ = new Subject < RdfType [ ] > ( ) ;
148+ const thing = {
149+ observeTypes : ( ) => observedTypes$ ,
150+ } as unknown as Thing ;
151+ page . rootInstance . receiveResource ( thing ) ;
152+ observedTypes$ . next ( [
153+ {
154+ label : 'Recipe' ,
155+ uri : 'http://schema.org/Recipe' ,
156+ } ,
157+ ] ) ;
155158 await page . waitForChanges ( ) ;
156159 expect ( page . root ?. innerHTML ) . toEqualHtml ( `
157160 <div>No matches</div>
@@ -166,14 +169,17 @@ describe('pos-switch', () => {
166169 <pos-case not if-typeof="http://schema.org/Video"><template><div>Not a Video</div></template></pos-case>
167170 </pos-switch>` ,
168171 } ) ;
169- await page . rootInstance . receiveResource ( {
170- types : ( ) => [
171- {
172- label : 'Recipe' ,
173- uri : 'http://schema.org/Recipe' ,
174- } ,
175- ] ,
176- } ) ;
172+ const observedTypes$ = new Subject < RdfType [ ] > ( ) ;
173+ const thing = {
174+ observeTypes : ( ) => observedTypes$ ,
175+ } as unknown as Thing ;
176+ page . rootInstance . receiveResource ( thing ) ;
177+ observedTypes$ . next ( [
178+ {
179+ label : 'Recipe' ,
180+ uri : 'http://schema.org/Recipe' ,
181+ } ,
182+ ] ) ;
177183 await page . waitForChanges ( ) ;
178184 expect ( page . root ?. innerHTML ) . toEqualHtml ( `
179185 <div>Not a Video</div>
0 commit comments