cozy-client 45.0.1 → 45.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/CozyClient.js +1 -1
- package/dist/mock.js +101 -27
- package/dist/mock.spec.js +32 -5
- package/package.json +2 -2
- package/types/mock.d.ts +23 -1
package/dist/CozyClient.js
CHANGED
|
@@ -2703,7 +2703,7 @@ var CozyClient = /*#__PURE__*/function () {
|
|
|
2703
2703
|
CozyClient.hooks = CozyClient.hooks || {};
|
|
2704
2704
|
CozyClient.fetchPolicies = _policies.default; //COZY_CLIENT_VERSION_PACKAGE in replaced by babel. See babel config
|
|
2705
2705
|
|
|
2706
|
-
CozyClient.version = "45.0
|
|
2706
|
+
CozyClient.version = "45.1.0";
|
|
2707
2707
|
|
|
2708
2708
|
_microee.default.mixin(CozyClient);
|
|
2709
2709
|
|
package/dist/mock.js
CHANGED
|
@@ -5,14 +5,20 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", {
|
|
6
6
|
value: true
|
|
7
7
|
});
|
|
8
|
-
exports.createMockClient = void 0;
|
|
8
|
+
exports.createFakeClient = exports.createMockClient = void 0;
|
|
9
9
|
|
|
10
10
|
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
11
11
|
|
|
12
|
+
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
|
13
|
+
|
|
14
|
+
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
|
15
|
+
|
|
12
16
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
13
17
|
|
|
14
18
|
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
|
|
15
19
|
|
|
20
|
+
var _merge = _interopRequireDefault(require("lodash/merge"));
|
|
21
|
+
|
|
16
22
|
var _CozyClient = _interopRequireDefault(require("./CozyClient"));
|
|
17
23
|
|
|
18
24
|
var _store = require("./store");
|
|
@@ -45,23 +51,48 @@ var fillQueryInsideClient = function fillQueryInsideClient(client, queryName, qu
|
|
|
45
51
|
};
|
|
46
52
|
|
|
47
53
|
var mockedQueryFromMockedRemoteData = function mockedQueryFromMockedRemoteData(remoteData) {
|
|
48
|
-
return function (
|
|
49
|
-
|
|
50
|
-
return {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
54
|
+
return /*#__PURE__*/function () {
|
|
55
|
+
var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(qdef) {
|
|
56
|
+
return _regenerator.default.wrap(function _callee$(_context) {
|
|
57
|
+
while (1) {
|
|
58
|
+
switch (_context.prev = _context.next) {
|
|
59
|
+
case 0:
|
|
60
|
+
if (remoteData) {
|
|
61
|
+
_context.next = 2;
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return _context.abrupt("return", {
|
|
66
|
+
data: null
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
case 2:
|
|
70
|
+
if (!remoteData[qdef.doctype]) {
|
|
71
|
+
_context.next = 6;
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return _context.abrupt("return", {
|
|
76
|
+
data: remoteData[qdef.doctype]
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
case 6:
|
|
80
|
+
return _context.abrupt("return", {
|
|
81
|
+
data: []
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
case 7:
|
|
85
|
+
case "end":
|
|
86
|
+
return _context.stop();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}, _callee);
|
|
90
|
+
}));
|
|
91
|
+
|
|
92
|
+
return function (_x) {
|
|
93
|
+
return _ref.apply(this, arguments);
|
|
94
|
+
};
|
|
95
|
+
}();
|
|
65
96
|
};
|
|
66
97
|
/**
|
|
67
98
|
* Creates a client suitable for use in tests
|
|
@@ -73,14 +104,59 @@ var mockedQueryFromMockedRemoteData = function mockedQueryFromMockedRemoteData(r
|
|
|
73
104
|
* @param {object} [options.queries] Prefill queries inside the store
|
|
74
105
|
* @param {object} [options.remote] Mock data from the server
|
|
75
106
|
* @param {object} [options.clientOptions] Options passed to the client
|
|
107
|
+
* @param {object} [options.clientFunctions] Functions to overide client functions
|
|
76
108
|
* @returns {CozyClient}
|
|
77
109
|
*/
|
|
78
110
|
|
|
79
111
|
|
|
80
|
-
var createMockClient = function createMockClient(
|
|
81
|
-
var
|
|
82
|
-
|
|
83
|
-
|
|
112
|
+
var createMockClient = function createMockClient() {
|
|
113
|
+
var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
114
|
+
queries = _ref2.queries,
|
|
115
|
+
remote = _ref2.remote,
|
|
116
|
+
clientOptions = _ref2.clientOptions,
|
|
117
|
+
clientFunctions = _ref2.clientFunctions;
|
|
118
|
+
|
|
119
|
+
var mockedQuery = jest.fn().mockImplementation(mockedQueryFromMockedRemoteData(remote));
|
|
120
|
+
var clientFunctionsMerge = (0, _merge.default)({
|
|
121
|
+
query: mockedQuery,
|
|
122
|
+
save: jest.fn(),
|
|
123
|
+
saveAll: jest.fn(),
|
|
124
|
+
stackClient: {
|
|
125
|
+
fetchJSON: jest.fn()
|
|
126
|
+
}
|
|
127
|
+
}, clientFunctions);
|
|
128
|
+
return createFakeClient({
|
|
129
|
+
queries: queries,
|
|
130
|
+
remote: remote,
|
|
131
|
+
clientOptions: clientOptions,
|
|
132
|
+
clientFunctions: clientFunctionsMerge
|
|
133
|
+
});
|
|
134
|
+
};
|
|
135
|
+
/**
|
|
136
|
+
* Creates a client with pre-filled store
|
|
137
|
+
* This can be useful for demo in documentation (e.g. storybook)
|
|
138
|
+
*
|
|
139
|
+
* - client.{query,save} are replaced with empty functions
|
|
140
|
+
* - client.stackClient.fetchJSON is replaced with empty functions
|
|
141
|
+
*
|
|
142
|
+
* @param {object} options Options
|
|
143
|
+
* @param {object} [options.queries] Prefill queries inside the store
|
|
144
|
+
* @param {object} [options.remote] Mock data from the server
|
|
145
|
+
* @param {object} [options.clientOptions] Options passed to the client
|
|
146
|
+
* @param {object} [options.clientFunctions] Functions to overide client functions useful for testing
|
|
147
|
+
* @returns {CozyClient}
|
|
148
|
+
*/
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
exports.createMockClient = createMockClient;
|
|
152
|
+
|
|
153
|
+
var createFakeClient = function createFakeClient() {
|
|
154
|
+
var _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
|
|
155
|
+
queries = _ref3.queries,
|
|
156
|
+
remote = _ref3.remote,
|
|
157
|
+
clientOptions = _ref3.clientOptions,
|
|
158
|
+
clientFunctions = _ref3.clientFunctions;
|
|
159
|
+
|
|
84
160
|
var client = new _CozyClient.default(clientOptions || {});
|
|
85
161
|
client.ensureStore();
|
|
86
162
|
|
|
@@ -92,11 +168,9 @@ var createMockClient = function createMockClient(_ref) {
|
|
|
92
168
|
fillQueryInsideClient(client, queryName, queryOptions);
|
|
93
169
|
}
|
|
94
170
|
|
|
95
|
-
client.query =
|
|
96
|
-
|
|
97
|
-
client.saveAll = jest.fn();
|
|
98
|
-
client.stackClient.fetchJSON = jest.fn();
|
|
171
|
+
client.query = mockedQueryFromMockedRemoteData(remote);
|
|
172
|
+
(0, _merge.default)(client, clientFunctions);
|
|
99
173
|
return client;
|
|
100
174
|
};
|
|
101
175
|
|
|
102
|
-
exports.
|
|
176
|
+
exports.createFakeClient = createFakeClient;
|
package/dist/mock.spec.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { createMockClient } from './mock'
|
|
1
|
+
import { createFakeClient, createMockClient } from './mock'
|
|
2
2
|
|
|
3
3
|
import { Q } from 'cozy-client'
|
|
4
4
|
|
|
5
|
-
describe('
|
|
5
|
+
describe('createFakeClient', () => {
|
|
6
6
|
const simpsonsFixture = [
|
|
7
7
|
{ _id: 'homer', name: 'Homer' },
|
|
8
8
|
{ _id: 'marge', name: 'Marge' }
|
|
9
9
|
]
|
|
10
10
|
|
|
11
11
|
it('should mock queries inside the store', () => {
|
|
12
|
-
const client =
|
|
12
|
+
const client = createFakeClient({
|
|
13
13
|
queries: {
|
|
14
14
|
simpsons: {
|
|
15
15
|
data: simpsonsFixture,
|
|
@@ -34,7 +34,7 @@ describe('createMockClient', () => {
|
|
|
34
34
|
})
|
|
35
35
|
|
|
36
36
|
it('should mock query with data passed in "remote" option', async () => {
|
|
37
|
-
const client =
|
|
37
|
+
const client = createFakeClient({
|
|
38
38
|
remote: {
|
|
39
39
|
'io.cozy.simpsons': simpsonsFixture
|
|
40
40
|
}
|
|
@@ -44,7 +44,7 @@ describe('createMockClient', () => {
|
|
|
44
44
|
})
|
|
45
45
|
|
|
46
46
|
it('should mock query even if the doctype has not been mocked', async () => {
|
|
47
|
-
const client =
|
|
47
|
+
const client = createFakeClient({
|
|
48
48
|
remote: {
|
|
49
49
|
'io.cozy.simpsons': simpsonsFixture
|
|
50
50
|
}
|
|
@@ -52,4 +52,31 @@ describe('createMockClient', () => {
|
|
|
52
52
|
const simpsons = await client.query(Q('io.cozy.adams'))
|
|
53
53
|
await expect(simpsons.data.map(x => x._id)).toEqual([])
|
|
54
54
|
})
|
|
55
|
+
|
|
56
|
+
it('should mock function with clientFunctions', async () => {
|
|
57
|
+
const client = createFakeClient({
|
|
58
|
+
clientFunctions: {
|
|
59
|
+
stackClient: {
|
|
60
|
+
fetchJSON: () =>
|
|
61
|
+
Promise.resolve({ data: [{ _id: 'homer', name: 'Homer' }] })
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
const simpsons = await client.stackClient.fetchJSON('io.cozy.simpsons')
|
|
67
|
+
expect(simpsons.data).toEqual([{ _id: 'homer', name: 'Homer' }])
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
describe('createMockClient', () => {
|
|
71
|
+
it('should mock basic client functions', async () => {
|
|
72
|
+
const client = createMockClient()
|
|
73
|
+
|
|
74
|
+
expect(JSON.stringify(client.query)).toEqual(JSON.stringify(jest.fn()))
|
|
75
|
+
expect(JSON.stringify(client.save)).toEqual(JSON.stringify(jest.fn()))
|
|
76
|
+
expect(JSON.stringify(client.saveAll)).toEqual(JSON.stringify(jest.fn()))
|
|
77
|
+
expect(JSON.stringify(client.stackClient.fetchJSON)).toEqual(
|
|
78
|
+
JSON.stringify(jest.fn())
|
|
79
|
+
)
|
|
80
|
+
})
|
|
81
|
+
})
|
|
55
82
|
})
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cozy-client",
|
|
3
|
-
"version": "45.0
|
|
3
|
+
"version": "45.1.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.node.js",
|
|
6
6
|
"browser": "dist/index.js",
|
|
@@ -75,5 +75,5 @@
|
|
|
75
75
|
"react-native-ios11-devicecheck": "https://github.com/cozy/react-native-devicecheck#app-attest-v0.1"
|
|
76
76
|
},
|
|
77
77
|
"sideEffects": false,
|
|
78
|
-
"gitHead": "
|
|
78
|
+
"gitHead": "383cfd5460d7bd48de61f135d9972c019bfc2f83"
|
|
79
79
|
}
|
package/types/mock.d.ts
CHANGED
|
@@ -8,11 +8,33 @@
|
|
|
8
8
|
* @param {object} [options.queries] Prefill queries inside the store
|
|
9
9
|
* @param {object} [options.remote] Mock data from the server
|
|
10
10
|
* @param {object} [options.clientOptions] Options passed to the client
|
|
11
|
+
* @param {object} [options.clientFunctions] Functions to overide client functions
|
|
11
12
|
* @returns {CozyClient}
|
|
12
13
|
*/
|
|
13
|
-
export function createMockClient({ queries, remote, clientOptions }
|
|
14
|
+
export function createMockClient({ queries, remote, clientOptions, clientFunctions }?: {
|
|
14
15
|
queries: object;
|
|
15
16
|
remote: object;
|
|
16
17
|
clientOptions: object;
|
|
18
|
+
clientFunctions: object;
|
|
19
|
+
}): CozyClient;
|
|
20
|
+
/**
|
|
21
|
+
* Creates a client with pre-filled store
|
|
22
|
+
* This can be useful for demo in documentation (e.g. storybook)
|
|
23
|
+
*
|
|
24
|
+
* - client.{query,save} are replaced with empty functions
|
|
25
|
+
* - client.stackClient.fetchJSON is replaced with empty functions
|
|
26
|
+
*
|
|
27
|
+
* @param {object} options Options
|
|
28
|
+
* @param {object} [options.queries] Prefill queries inside the store
|
|
29
|
+
* @param {object} [options.remote] Mock data from the server
|
|
30
|
+
* @param {object} [options.clientOptions] Options passed to the client
|
|
31
|
+
* @param {object} [options.clientFunctions] Functions to overide client functions useful for testing
|
|
32
|
+
* @returns {CozyClient}
|
|
33
|
+
*/
|
|
34
|
+
export function createFakeClient({ queries, remote, clientOptions, clientFunctions }?: {
|
|
35
|
+
queries: object;
|
|
36
|
+
remote: object;
|
|
37
|
+
clientOptions: object;
|
|
38
|
+
clientFunctions: object;
|
|
17
39
|
}): CozyClient;
|
|
18
40
|
import CozyClient from "./CozyClient";
|