Skip to content

Commit 4d405a7

Browse files
author
Kasey Powers
committed
feat(api-core): helper function for settings api
1 parent f90803a commit 4d405a7

File tree

2 files changed

+97
-52
lines changed

2 files changed

+97
-52
lines changed

packages/api-core/src/resources/settings.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import AvApi from '../api';
22

33
export default class AvSettings extends AvApi {
4-
constructor({ http, promise, merge, config }) {
4+
constructor({ http, promise, merge, avUsers, config }) {
55
const options = Object.assign(
66
{
77
path: 'api/utils',
88
name: 'settings',
9+
sessionBust: false,
10+
pageBust: true,
911
},
1012
config
1113
);
@@ -15,5 +17,41 @@ export default class AvSettings extends AvApi {
1517
merge,
1618
config: options,
1719
});
20+
this.avUsers = avUsers;
21+
}
22+
23+
getApplication(applicationId, config) {
24+
if (!applicationId) {
25+
throw new Error('must define applicationId');
26+
}
27+
return this.avUsers.me().then(user => {
28+
const queryConfig = this.addParams(
29+
{ applicationId, userId: user.id },
30+
config
31+
);
32+
return this.query(queryConfig);
33+
});
34+
}
35+
36+
setApplication(applicaitonId, data, config) {
37+
if (
38+
typeof applicaitonId !== 'string' &&
39+
typeof applicaitonId !== 'number'
40+
) {
41+
config = data;
42+
data = applicaitonId;
43+
applicaitonId = '';
44+
}
45+
46+
if (!applicaitonId && (!data.scope || !data.scope.applicationId)) {
47+
throw new Error('must set applicationId in settings call');
48+
}
49+
50+
return this.avUsers.me().then(user => {
51+
data.scope = data.scope || {};
52+
data.scope.applicationId = applicaitonId;
53+
data.scope.userId = user.id;
54+
return this.update(data, config);
55+
});
1856
}
1957
}

packages/api-core/src/resources/tests/settings.test.js

Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,81 +3,88 @@ import AvSettings from '../settings';
33
const mockHttp = jest.fn(() => Promise.resolve({}));
44
const mockMerge = jest.fn((...args) => Object.assign(...args));
55

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)),
1113
};
1214

1315
describe('AvSettings', () => {
1416
let api;
1517

16-
test('should be defined', () => {
18+
beforeEach(() => {
1719
api = new AvSettings({
1820
http: mockHttp,
1921
promise: Promise,
2022
merge: mockMerge,
23+
avUsers: mockAvUsers,
2124
config: {},
2225
});
23-
expect(api).toBeDefined();
2426
});
2527

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', () => {
3229
expect(api).toBeDefined();
3330
});
3431

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();
4050
});
41-
expect(api.getUrl(api.config(mockConfig))).toBe('/api/utils/v1/settings');
4251
});
4352

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();
5056
});
5157

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+
);
5870

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+
});
6375

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();
7084
});
71-
const data = {
72-
scope: {
73-
applicationId: '123',
74-
userId: 'myUser',
75-
},
76-
key: 'value',
77-
};
7885

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+
});
8289
});
8390
});

0 commit comments

Comments
 (0)