@@ -3,81 +3,88 @@ import AvSettings from '../settings';
3
3
const mockHttp = jest . fn ( ( ) => Promise . resolve ( { } ) ) ;
4
4
const mockMerge = jest . fn ( ( ...args ) => Object . assign ( ...args ) ) ;
5
5
6
- const mockConfig = {
7
- scope : {
8
- applicationId : '123' ,
9
- userId : 'myUser' ,
10
- } ,
6
+ const testAppId = 'testApplicationId' ;
7
+
8
+ const mockUser = {
9
+ id : 'mockUserId' ,
10
+ } ;
11
+ const mockAvUsers = {
12
+ me : jest . fn ( ( ) => Promise . resolve ( mockUser ) ) ,
11
13
} ;
12
14
13
15
describe ( 'AvSettings' , ( ) => {
14
16
let api ;
15
17
16
- test ( 'should be defined' , ( ) => {
18
+ beforeEach ( ( ) => {
17
19
api = new AvSettings ( {
18
20
http : mockHttp ,
19
21
promise : Promise ,
20
22
merge : mockMerge ,
23
+ avUsers : mockAvUsers ,
21
24
config : { } ,
22
25
} ) ;
23
- expect ( api ) . toBeDefined ( ) ;
24
26
} ) ;
25
27
26
- test ( 'should handle no config passed in' , ( ) => {
27
- api = new AvSettings ( {
28
- http : mockHttp ,
29
- promise : Promise ,
30
- merge : mockMerge ,
31
- } ) ;
28
+ test ( 'should be defined' , ( ) => {
32
29
expect ( api ) . toBeDefined ( ) ;
33
30
} ) ;
34
31
35
- test ( 'url should be correct' , ( ) => {
36
- api = new AvSettings ( {
37
- http : mockHttp ,
38
- promise : Promise ,
39
- merge : mockMerge ,
32
+ describe ( 'getApplication' , ( ) => {
33
+ beforeEach ( ( ) => {
34
+ api . query = jest . fn ( ) ;
35
+ } ) ;
36
+ test ( 'should call avUsers.me and use in query' , async ( ) => {
37
+ const expectedQuery = {
38
+ params : {
39
+ applicationId : testAppId ,
40
+ userId : mockUser . id ,
41
+ } ,
42
+ } ;
43
+ await api . getApplication ( testAppId ) ;
44
+ expect ( mockAvUsers . me ) . toHaveBeenCalled ( ) ;
45
+ expect ( api . query ) . toHaveBeenCalledWith ( expectedQuery ) ;
46
+ } ) ;
47
+
48
+ test ( 'should throw error if no applicationId passed in' , ( ) => {
49
+ expect ( ( ) => api . getApplication ( ) ) . toThrow ( ) ;
40
50
} ) ;
41
- expect ( api . getUrl ( api . config ( mockConfig ) ) ) . toBe ( '/api/utils/v1/settings' ) ;
42
51
} ) ;
43
52
44
- test ( 'query() should be called with params' , ( ) => {
45
- api = new AvSettings ( {
46
- http : mockHttp ,
47
- promise : Promise ,
48
- merge : mockMerge ,
49
- config : { } ,
53
+ describe ( 'setApplication' , ( ) => {
54
+ beforeEach ( ( ) => {
55
+ api . update = jest . fn ( ) ;
50
56
} ) ;
51
57
52
- const data = {
53
- params : {
54
- applicationId : '123' ,
55
- userId : 'myUser' ,
56
- } ,
57
- } ;
58
+ test ( 'should add applicationId and user.me to scope' , async ( ) => {
59
+ const testData = { key : 'value' } ;
60
+ const testConfig = { } ;
61
+ const expectedUpdate = Object . assign (
62
+ {
63
+ scope : {
64
+ applicationId : testAppId ,
65
+ userId : mockUser . id ,
66
+ } ,
67
+ } ,
68
+ testData
69
+ ) ;
58
70
59
- api . query = jest . fn ( ) ;
60
- api . query ( data ) ;
61
- expect ( api . query ) . toHaveBeenLastCalledWith ( data ) ;
62
- } ) ;
71
+ await api . setApplication ( testAppId , testData , testConfig ) ;
72
+ expect ( mockAvUsers . me ) . toHaveBeenCalled ( ) ;
73
+ expect ( api . update ) . toHaveBeenCalledWith ( expectedUpdate , testConfig ) ;
74
+ } ) ;
63
75
64
- test ( 'update() should be called with scope' , async ( ) => {
65
- api = new AvSettings ( {
66
- http : mockHttp ,
67
- promise : Promise ,
68
- merge : mockMerge ,
69
- config : { } ,
76
+ test ( 'should not throw error if application id passed in as arugment' , ( ) => {
77
+ expect ( ( ) => api . setApplication ( testAppId , { } ) ) . not . toThrow ( ) ;
78
+ } ) ;
79
+
80
+ test ( 'should not throw error if applicationId in scope' , ( ) => {
81
+ expect ( ( ) =>
82
+ api . setApplication ( { scope : { applicationId : testAppId } } )
83
+ ) . not . toThrow ( ) ;
70
84
} ) ;
71
- const data = {
72
- scope : {
73
- applicationId : '123' ,
74
- userId : 'myUser' ,
75
- } ,
76
- key : 'value' ,
77
- } ;
78
85
79
- api . update = jest . fn ( ) ;
80
- api . update ( data ) ;
81
- expect ( api . update ) . toHaveBeenLastCalledWith ( data ) ;
86
+ test ( 'should throw error if no applicationId in argument or data' , ( ) => {
87
+ expect ( ( ) => api . setApplication ( ) ) . toThrow ( ) ;
88
+ } ) ;
82
89
} ) ;
83
90
} ) ;
0 commit comments