1+ import { TestApplication , TestApplication2 } from './TestApplication' ;
2+ import { DataContext , UserService } from '@themost/data' ;
3+ import { firstValueFrom } from 'rxjs' ;
4+
5+ describe ( 'DataContext' , ( ) => {
6+ let app : TestApplication ;
7+ let context : DataContext ;
8+ beforeAll ( ( done ) => {
9+ app = new TestApplication2 ( ) ;
10+ app . useService ( UserService ) ;
11+ context = app . createContext ( ) ;
12+ return done ( ) ;
13+ } ) ;
14+ afterAll ( async ( ) => {
15+ await context . finalizeAsync ( ) ;
16+ await app . finalize ( ) ;
17+ } ) ;
18+
19+ it ( 'should serialize context as empty object' , async ( ) => {
20+ context . setUser ( {
21+ name : 'alexis.rees@example.com' ,
22+ } ) ;
23+ const user = await firstValueFrom ( context . user$ ) ;
24+ const str = JSON . stringify ( context ) ;
25+ expect ( str ) . toEqual ( '{}' ) ;
26+ } )
27+
28+ it ( 'should have user$' , async ( ) => {
29+ context . setUser ( null ) ;
30+ const user = await firstValueFrom ( context . user$ ) ;
31+ expect ( user ?. name ) . toBeFalsy ( ) ;
32+ context . setUser ( {
33+ name : 'alexis.rees@example.com'
34+ } ) ;
35+ const user2 = await firstValueFrom ( context . user$ ) ;
36+ expect ( user2 ?. name ) . toBe ( 'alexis.rees@example.com' ) ;
37+ context . switchUser ( {
38+ name : 'james.may@example.com'
39+ } ) ;
40+ const user3 = await firstValueFrom ( context . user$ ) ;
41+ expect ( user3 ?. name ) . toBe ( 'james.may@example.com' ) ;
42+ } ) ;
43+
44+ it ( 'should have interactiveUser$' , async ( ) => {
45+ let user = await firstValueFrom ( context . interactiveUser$ ) ;
46+ expect ( user ?. name ) . toBeFalsy ( ) ;
47+ context . setInteractiveUser ( {
48+ name : 'alexis.rees@example.com'
49+ } ) ;
50+ user = await firstValueFrom ( context . interactiveUser$ ) ;
51+ expect ( user ?. name ) . toBe ( 'alexis.rees@example.com' ) ;
52+ } ) ;
53+
54+ it ( 'should use different context' , async ( ) => {
55+ context . switchUser ( {
56+ name : 'james.may@example.com'
57+ } ) ;
58+ let user = await firstValueFrom ( context . user$ ) ;
59+ expect ( user ?. name ) . toBe ( 'james.may@example.com' ) ;
60+
61+ const otherContext = app . createContext ( ) ;
62+ otherContext . setUser ( {
63+ name : 'alexis.rees@example.com'
64+ } ) ;
65+ user = await firstValueFrom ( otherContext . user$ ) ;
66+ expect ( user ?. name ) . toBe ( 'alexis.rees@example.com' ) ;
67+ user = await firstValueFrom ( context . user$ ) ;
68+ expect ( user ?. name ) . toBe ( 'james.may@example.com' ) ;
69+
70+ } ) ;
71+
72+ it ( 'should set user' , async ( ) => {
73+ context . user = null ;
74+ let user = await firstValueFrom ( context . user$ ) ;
75+ expect ( user ?. name ) . toBeFalsy ( ) ;
76+ context . user = {
77+ name : 'alexis.rees@example.com'
78+ } ;
79+ user = await firstValueFrom ( context . user$ ) ;
80+ expect ( user ?. name ) . toBe ( 'alexis.rees@example.com' ) ;
81+ context . user = {
82+ name : 'james.may@example.com'
83+ } ;
84+ user = await firstValueFrom ( context . user$ ) ;
85+ expect ( user ?. name ) . toBe ( 'james.may@example.com' ) ;
86+ } ) ;
87+
88+ it ( 'should set user name' , async ( ) => {
89+ context . user = null ;
90+ let user = await firstValueFrom ( context . user$ ) ;
91+ expect ( user ?. name ) . toBeFalsy ( ) ;
92+ context . user = {
93+ name : 'alexis.rees@example.com'
94+ } ;
95+ expect ( context . user . name ) . toBe ( 'alexis.rees@example.com' ) ;
96+ user = await firstValueFrom ( context . user$ ) ;
97+ expect ( user ?. name ) . toBe ( 'alexis.rees@example.com' ) ;
98+ context . user . name = 'james.may@example.com' ;
99+ user = await firstValueFrom ( context . user$ ) ;
100+ expect ( user ?. name ) . toBe ( 'james.may@example.com' ) ;
101+ } ) ;
102+
103+ } ) ;
0 commit comments