@@ -2,6 +2,7 @@ import { graph, sym, IndexedFormula, quad } from "rdflib";
22import { PodOsSession } from "../authentication" ;
33import { Thing } from "./Thing" ;
44import { Store } from "../Store" ;
5+ import { Subscription } from "rxjs" ;
56
67describe ( "Thing" , function ( ) {
78 describe ( "types" , ( ) => {
@@ -84,124 +85,138 @@ describe("Thing", function () {
8485 } ) ;
8586
8687 describe ( "observeTypes" , ( ) => {
87- it ( "pushes existing value immediately and changed values until unsubscribe unless irrelevant" , ( ) => {
88- const internalStore = graph ( ) ;
88+ let internalStore : IndexedFormula ,
89+ uri : string ,
90+ subscriber : jest . Mock ,
91+ subscription : Subscription ;
92+
93+ beforeEach ( ( ) => {
94+ // Given a store with a type statement about a URI
95+ internalStore = graph ( ) ;
96+ const mockSession = { } as unknown as PodOsSession ;
97+ const store = new Store ( mockSession , undefined , undefined , internalStore ) ;
98+ uri = "http://recipe.test/0" ;
8999 internalStore . add (
90100 quad (
91- sym ( "http://recipe.test/0" ) ,
101+ sym ( uri ) ,
92102 sym ( "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" ) ,
93103 sym ( "http://schema.org/Recipe" ) ,
94104 ) ,
95105 ) ;
96- const store = new Store (
97- { } as PodOsSession ,
98- undefined ,
99- undefined ,
100- internalStore ,
101- ) ;
102- const subscriber = jest . fn ( ) ;
103- const thing = new Thing ( "http://recipe.test/0" , store ) ;
106+
107+ // and a Thing
108+ const thing = new Thing ( uri , store ) ;
109+
110+ // and a subscription to changes in types
111+ subscriber = jest . fn ( ) ;
104112 const observable = thing . observeTypes ( ) ;
105- const subscription = observable . subscribe ( subscriber ) ;
113+ subscription = observable . subscribe ( subscriber ) ;
114+ } ) ;
106115
107- // Existing value
116+ it ( "pushes existing value immediately" , ( ) => {
108117 expect ( subscriber ) . toHaveBeenCalledTimes ( 1 ) ;
118+ expect ( subscriber . mock . calls ) . toEqual ( [
119+ [
120+ [
121+ {
122+ uri : "http://schema.org/Recipe" ,
123+ label : "Recipe" ,
124+ } ,
125+ ] ,
126+ ] ,
127+ ] ) ;
128+ } ) ;
129+
130+ it ( "ignores irrelevant statements about another resource" , ( ) => {
109131 internalStore . add (
110132 quad (
111- sym ( "http://recipe.test/0 " ) ,
133+ sym ( "http://recipe.test/a-different-recipe " ) ,
112134 sym ( "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" ) ,
113- sym ( "http://example.com/Recipe2 " ) ,
135+ sym ( "http://schema.org/Recipe " ) ,
114136 ) ,
115137 ) ;
116- // Irrelevant statement about another resource
138+ expect ( subscriber ) . toHaveBeenCalledTimes ( 1 ) ;
139+ } ) ;
140+
141+ it ( "updates value when added" , ( ) => {
117142 internalStore . add (
118143 quad (
119- sym ( "http://recipe.test/a-different-recipe" ) ,
144+ sym ( uri ) ,
120145 sym ( "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" ) ,
121- sym ( "http://schema.org/Recipe " ) ,
146+ sym ( "http://example.com/Recipe2 " ) ,
122147 ) ,
123148 ) ;
124149 expect ( subscriber ) . toHaveBeenCalledTimes ( 2 ) ;
125- // Changed by removal
150+ expect ( subscriber . mock . lastCall ) . toEqual ( [
151+ [
152+ {
153+ uri : "http://schema.org/Recipe" ,
154+ label : "Recipe" ,
155+ } ,
156+ {
157+ uri : "http://example.com/Recipe2" ,
158+ label : "Recipe2" ,
159+ } ,
160+ ] ,
161+ ] ) ;
162+ } ) ;
163+
164+ it ( "updates value when removed" , ( ) => {
126165 internalStore . removeStatement (
127166 quad (
128- sym ( "http://recipe.test/0" ) ,
167+ sym ( uri ) ,
129168 sym ( "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" ) ,
130- sym ( "http://example.com/Recipe2 " ) ,
169+ sym ( "http://schema.org/Recipe " ) ,
131170 ) ,
132171 ) ;
133- expect ( subscriber ) . toHaveBeenCalledTimes ( 3 ) ;
134- // Changed by added subclass
172+ expect ( subscriber ) . toHaveBeenCalledTimes ( 2 ) ;
173+ expect ( subscriber . mock . lastCall ) . toEqual ( [ [ ] ] ) ;
174+ } ) ;
175+
176+ it ( "updates when a subclass is added" , ( ) => {
135177 internalStore . add (
136178 quad (
137179 sym ( "http://schema.org/Recipe" ) ,
138180 sym ( "http://www.w3.org/2000/01/rdf-schema#subClassOf" ) ,
139181 sym ( "http://schema.org/Thing" ) ,
140182 ) ,
141183 ) ;
142- expect ( subscriber ) . toHaveBeenCalledTimes ( 4 ) ;
143- // Irrelevant subclass doesn't change types
184+ expect ( subscriber ) . toHaveBeenCalledTimes ( 2 ) ;
185+ expect ( subscriber . mock . lastCall ) . toEqual ( [
186+ [
187+ {
188+ uri : "http://schema.org/Recipe" ,
189+ label : "Recipe" ,
190+ } ,
191+ {
192+ uri : "http://schema.org/Thing" ,
193+ label : "Thing" ,
194+ } ,
195+ ] ,
196+ ] ) ;
197+ } ) ;
198+
199+ it ( "does not update if a new subclass is irrelevant" , ( ) => {
144200 internalStore . add (
145201 quad (
146202 sym ( "http://schema.org/Video" ) ,
147203 sym ( "http://www.w3.org/2000/01/rdf-schema#subClassOf" ) ,
148204 sym ( "http://schema.org/Thing" ) ,
149205 ) ,
150206 ) ;
151- expect ( subscriber ) . toHaveBeenCalledTimes ( 4 ) ;
152- expect ( subscriber . mock . calls ) . toEqual ( [
153- [
154- [
155- {
156- uri : "http://schema.org/Recipe" ,
157- label : "Recipe" ,
158- } ,
159- ] ,
160- ] ,
161- [
162- [
163- {
164- uri : "http://schema.org/Recipe" ,
165- label : "Recipe" ,
166- } ,
167- {
168- uri : "http://example.com/Recipe2" ,
169- label : "Recipe2" ,
170- } ,
171- ] ,
172- ] ,
173- [
174- [
175- {
176- uri : "http://schema.org/Recipe" ,
177- label : "Recipe" ,
178- } ,
179- ] ,
180- ] ,
181- [
182- [
183- {
184- uri : "http://schema.org/Recipe" ,
185- label : "Recipe" ,
186- } ,
187- {
188- uri : "http://schema.org/Thing" ,
189- label : "Thing" ,
190- } ,
191- ] ,
192- ] ,
193- ] ) ;
207+ expect ( subscriber ) . toHaveBeenCalledTimes ( 1 ) ;
208+ } ) ;
194209
195- // Stop listening to ignore future changes
210+ it ( "does not update after unsubscribe" , ( ) => {
196211 subscription . unsubscribe ( ) ;
197212 internalStore . removeStatement (
198213 quad (
199- sym ( "http://recipe.test/0" ) ,
214+ sym ( uri ) ,
200215 sym ( "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" ) ,
201216 sym ( "http://schema.org/Recipe" ) ,
202217 ) ,
203218 ) ;
204- expect ( subscriber ) . toHaveBeenCalledTimes ( 4 ) ;
219+ expect ( subscriber ) . toHaveBeenCalledTimes ( 1 ) ;
205220 } ) ;
206221 } ) ;
207222} ) ;
0 commit comments