@@ -3,10 +3,14 @@ import { BasicEntityGetter, DataCache, EntityGetter, EntityView, ServerError } f
3
3
import { from , throwError } from "rxjs" ;
4
4
import { FakeModel , FakeParams } from "../test/fake-model" ;
5
5
6
+ const fake1 = { id : "1" , parentId : "parent-1" , state : "active" , name : "Fake1" } ;
7
+ const fake2 = { id : "1" , parentId : "parent-1" , state : "running" , name : "Fake1" } ;
8
+ const fake3 = { id : "1" , parentId : "parent-1" , state : "completed" , name : "Fake1" } ;
9
+
6
10
const data = [
7
11
{ id : "1" , state : "active" , name : "Fake1" } ,
8
12
{ id : "1" , state : "running" , name : "Fake1" } ,
9
- { id : "1" , state : "complated " , name : "Fake1" } ,
13
+ { id : "1" , state : "completed " , name : "Fake1" } ,
10
14
] ;
11
15
12
16
describe ( "EntityView" , ( ) => {
@@ -30,7 +34,7 @@ describe("EntityView", () => {
30
34
cache : ( ) => cache ,
31
35
getter : getter ,
32
36
} ) ;
33
- view . params = { id : "1" } ;
37
+ view . params = { parentId : "parent-1" , id : "1" } ;
34
38
} ) ;
35
39
36
40
afterEach ( ( ) => {
@@ -43,68 +47,67 @@ describe("EntityView", () => {
43
47
view . fetch ( ) ;
44
48
tick ( ) ;
45
49
expect ( dataSpy ) . toHaveBeenCalledTimes ( 1 ) ;
46
- expect ( item ! . toJS ( ) ) . toEqual ( data [ 0 ] ) ;
47
- expect ( dataSpy ) . toHaveBeenCalledWith ( { id : "1" } ) ;
50
+ expect ( item ! . toJS ( ) ) . toEqual ( fake1 ) ;
51
+ expect ( dataSpy ) . toHaveBeenCalledWith ( { parentId : "parent-1" , id : "1" } ) ;
48
52
} ) ) ;
49
53
50
54
it ( "should use the cached value" , fakeAsync ( ( ) => {
51
- cache . addItem ( new FakeModel ( { id : "1" , state : "creating" , name : "Fake1" } ) ) ;
55
+ cache . addItem ( new FakeModel ( { id : "1" , parentId : "parent-1" , state : "creating" , name : "Fake1" } ) ) ;
52
56
53
57
view . item . subscribe ( x => item = x ) ;
54
58
view . fetch ( ) ;
55
59
expect ( item ) . not . toBeFalsy ( ) ;
56
- expect ( item ! . toJS ( ) ) . toEqual ( { id : "1" , state : "creating" , name : "Fake1" } ) ;
60
+ expect ( item ! . toJS ( ) ) . toEqual ( { id : "1" , parentId : "parent-1" , state : "creating" , name : "Fake1" } ) ;
57
61
58
62
tick ( ) ; // This should be the return from the fetched data
59
- expect ( item ! . toJS ( ) ) . toEqual ( data [ 0 ] ) ;
63
+ expect ( item ! . toJS ( ) ) . toEqual ( fake1 ) ;
60
64
} ) ) ;
61
65
62
66
it ( "Update the data when refreshing" , fakeAsync ( ( ) => {
63
67
view . item . subscribe ( x => item = x ) ;
64
68
view . fetch ( ) ;
65
69
tick ( ) ;
66
- expect ( item ! . toJS ( ) ) . toEqual ( data [ 0 ] ) ;
70
+ expect ( item ! . toJS ( ) ) . toEqual ( fake1 ) ;
67
71
68
72
view . refresh ( ) ;
69
73
tick ( ) ;
70
- expect ( item ! . toJS ( ) ) . toEqual ( data [ 1 ] ) ;
74
+ expect ( item ! . toJS ( ) ) . toEqual ( fake2 ) ;
71
75
72
76
view . refresh ( ) ;
73
77
tick ( ) ;
74
- expect ( item ! . toJS ( ) ) . toEqual ( data [ 2 ] ) ;
78
+ expect ( item ! . toJS ( ) ) . toEqual ( fake3 ) ;
75
79
expect ( dataSpy ) . toHaveBeenCalledTimes ( 3 ) ;
76
80
} ) ) ;
77
81
78
82
it ( "Update the params" , fakeAsync ( ( ) => {
79
83
view . item . subscribe ( x => item = x ) ;
80
- view . params = { id : "2" } ;
84
+ view . params = { parentId : "parent-1" , id : "2" } ;
81
85
view . fetch ( ) ;
82
86
tick ( ) ;
83
- expect ( item ! . toJS ( ) ) . toEqual ( data [ 0 ] ) ;
84
- expect ( dataSpy ) . toHaveBeenCalledWith ( { id : "2" } ) ;
87
+ expect ( item ! . toJS ( ) ) . toEqual ( fake1 ) ;
88
+ expect ( dataSpy ) . toHaveBeenCalledWith ( { parentId : "parent-1" , id : "2" } ) ;
85
89
} ) ) ;
86
90
87
91
it ( "Update the params" , fakeAsync ( ( ) => {
88
92
view . item . subscribe ( x => item = x ) ;
89
- view . params = { id : "2" } ;
93
+ view . params = { parentId : "parent-1" , id : "2" } ;
90
94
view . fetch ( ) ;
91
95
tick ( ) ;
92
- expect ( item ! . toJS ( ) ) . toEqual ( data [ 0 ] ) ;
93
- expect ( dataSpy ) . toHaveBeenCalledWith ( { id : "2" } ) ;
96
+ expect ( item ! . toJS ( ) ) . toEqual ( fake1 ) ;
97
+ expect ( dataSpy ) . toHaveBeenCalledWith ( { parentId : "parent-1" , id : "2" } ) ;
94
98
} ) ) ;
95
99
96
100
it ( "Update the cache should update the item" , fakeAsync ( ( ) => {
97
101
view . item . subscribe ( x => item = x ) ;
98
102
view . fetch ( ) ;
99
103
tick ( ) ;
100
- expect ( item ! . toJS ( ) ) . toEqual ( data [ 0 ] ) ;
101
- expect ( item ) . toEqualImmutable ( new FakeModel ( data [ 0 ] ) ) ;
104
+ expect ( item ! . toJS ( ) ) . toEqual ( fake1 ) ;
102
105
expect ( dataSpy ) . toHaveBeenCalledTimes ( 1 ) ;
103
- expect ( dataSpy ) . toHaveBeenCalledWith ( { id : "1" } ) ;
106
+ expect ( dataSpy ) . toHaveBeenCalledWith ( { parentId : "parent-1" , id : "1" } ) ;
104
107
105
- cache . addItem ( new FakeModel ( data [ 2 ] ) ) ;
108
+ cache . addItem ( new FakeModel ( { ... data [ 2 ] , parentId : "parent-1" } ) ) ;
106
109
tick ( ) ;
107
- expect ( item ! . toJS ( ) ) . toEqual ( data [ 2 ] ) ;
110
+ expect ( item ! . toJS ( ) ) . toEqual ( fake3 ) ;
108
111
} ) ) ;
109
112
110
113
describe ( "When it return a 404 error" , ( ) => {
@@ -115,7 +118,7 @@ describe("EntityView", () => {
115
118
item = null ;
116
119
deleted = null ;
117
120
const responses = [
118
- from ( Promise . resolve ( data [ 0 ] ) ) ,
121
+ from ( Promise . resolve ( fake1 ) ) ,
119
122
throwError ( new ServerError ( { status : 404 , message : "404 not found" } as any ) ) ,
120
123
] ;
121
124
dataSpy = jasmine . createSpy ( "supplyDataSpy" )
@@ -130,7 +133,7 @@ describe("EntityView", () => {
130
133
cache : ( ) => cache ,
131
134
getter : getter ,
132
135
} ) ;
133
- view . params = { id : "1" } ;
136
+ view . params = { parentId : "parent-1" , id : "1" } ;
134
137
} ) ;
135
138
136
139
it ( "should not send to delete if loading for the first time and I get a 404" , fakeAsync ( ( ) => {
@@ -143,7 +146,7 @@ describe("EntityView", () => {
143
146
cache : ( ) => cache ,
144
147
getter : getter ,
145
148
} ) ;
146
- view . params = { id : "1" } ;
149
+ view . params = { parentId : "parent-1" , id : "1" } ;
147
150
148
151
view . item . subscribe ( x => item = x ) ;
149
152
view . deleted . subscribe ( ( x ) => deleted = x ) ;
@@ -158,7 +161,7 @@ describe("EntityView", () => {
158
161
cache : ( ) => cache ,
159
162
getter : getter ,
160
163
} ) ;
161
- otherView . params = { id : "1" } ;
164
+ otherView . params = { parentId : "parent-1" , id : "1" } ;
162
165
163
166
view . item . subscribe ( x => item = x ) ;
164
167
cache . deleted . subscribe ( x => deleted = x ) ;
@@ -184,7 +187,7 @@ describe("EntityView", () => {
184
187
// expect(deleted).toBe(null);
185
188
186
189
// This fetch should return an 404 error
187
- view . params = { id : "2" } ;
190
+ view . params = { parentId : "parent-1" , id : "2" } ;
188
191
view . fetch ( ) ;
189
192
tick ( ) ;
190
193
0 commit comments