@@ -6,32 +6,28 @@ describe('Project Storage', () => {
6
6
let storageProjects : Project [ ] ;
7
7
let projectsIdentifier : string ;
8
8
let serverProjects : Project [ ] ;
9
+ let testProject : Project ;
10
+ let localStorageGetItemMock ;
9
11
10
12
beforeEach ( ( ) => {
11
13
projectsIdentifier = 'projectsSelected' ;
12
14
15
+ testProject = {
16
+ customer : {
17
+ name : 'ioet Inc. (was E&Y)' ,
18
+ } ,
19
+ id : 'f3630e59-9408-497e-945b-848112bd5a44' ,
20
+ name : 'Time Tracker' ,
21
+ customer_id : '20c96c4d-5e26-4426-a704-8bdd98c83319' ,
22
+ status : 'active' ,
23
+ } ;
24
+
13
25
storageProjects = [
14
- {
15
- customer : {
16
- name : 'ioet Inc. (was E&Y)' ,
17
- } ,
18
- id : 'f3630e59-9408-497e-945b-848112bd5a44' ,
19
- name : 'Time Tracker' ,
20
- customer_id : '20c96c4d-5e26-4426-a704-8bdd98c83319' ,
21
- status : 'active' ,
22
- }
26
+ testProject
23
27
] ;
24
28
25
29
serverProjects = [
26
- {
27
- customer : {
28
- name : 'ioet Inc. (was E&Y)' ,
29
- } ,
30
- id : 'f3630e59-9408-497e-945b-848112bd5a44' ,
31
- name : 'Time Tracker' ,
32
- customer_id : '20c96c4d-5e26-4426-a704-8bdd98c83319' ,
33
- status : 'active' ,
34
- } ,
30
+ testProject ,
35
31
{
36
32
customer : {
37
33
name : 'No Matter Name' ,
@@ -43,21 +39,19 @@ describe('Project Storage', () => {
43
39
}
44
40
] ;
45
41
42
+ localStorageGetItemMock = spyOn ( localStorage , 'getItem' ) . and . returnValue ( JSON . stringify ( storageProjects ) ) ;
43
+ spyOn ( localStorage , 'setItem' ) ;
44
+
46
45
} ) ;
47
46
48
47
it ( 'If exists projects in localStorage and the server returns the same project, should keep the same localStorage variables' , ( ) => {
49
- spyOn ( localStorage , 'getItem' ) . and . returnValue ( JSON . stringify ( storageProjects ) ) ;
50
- spyOn ( localStorage , 'setItem' ) ;
51
-
52
48
updateProjectStorage ( serverProjects ) ;
53
49
54
50
expect ( localStorage . setItem ) . toHaveBeenCalledWith ( projectsIdentifier , JSON . stringify ( storageProjects ) ) ;
55
51
} ) ;
56
52
57
53
it ( 'If exists projects in localStorage and the server does not return that project, should update the localStorage variable' , ( ) => {
58
54
serverProjects . shift ( ) ;
59
- spyOn ( localStorage , 'getItem' ) . and . returnValue ( JSON . stringify ( storageProjects ) ) ;
60
- spyOn ( localStorage , 'setItem' ) ;
61
55
62
56
updateProjectStorage ( serverProjects ) ;
63
57
@@ -66,8 +60,6 @@ describe('Project Storage', () => {
66
60
67
61
it ( 'If Server projects is empty, should not update the localStorage' , ( ) => {
68
62
serverProjects = [ ] ;
69
- spyOn ( localStorage , 'getItem' ) . and . returnValue ( JSON . stringify ( storageProjects ) ) ;
70
- spyOn ( localStorage , 'setItem' ) ;
71
63
72
64
updateProjectStorage ( serverProjects ) ;
73
65
@@ -76,7 +68,8 @@ describe('Project Storage', () => {
76
68
77
69
it ( 'If variables does not exists on localStorage, getProjectsOnStorage should return undefined' , ( ) => {
78
70
projectsIdentifier = 'no-matter-identifier' ;
79
- spyOn ( localStorage , 'getItem' ) . and . returnValue ( undefined ) ;
71
+
72
+ localStorageGetItemMock . and . returnValue ( undefined ) ;
80
73
81
74
const projects = getProjectsOnStorage ( projectsIdentifier ) ;
82
75
@@ -85,7 +78,7 @@ describe('Project Storage', () => {
85
78
86
79
it ( 'If variables not exists on localStorage, getProjectsOnStorage should return an array of Projects' , ( ) => {
87
80
const storageProjectsString = JSON . stringify ( storageProjects ) ;
88
- spyOn ( localStorage , 'getItem' ) . and . returnValue ( storageProjectsString ) ;
81
+ localStorageGetItemMock . and . returnValue ( storageProjectsString ) ;
89
82
90
83
const projects = getProjectsOnStorage ( projectsIdentifier ) ;
91
84
0 commit comments