@dhis2/app-service-data 3.2.4 → 3.2.8

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.
@@ -10,8 +10,8 @@ function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return
10
10
 
11
11
  function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
12
12
 
13
- describe('Testing custom data provider and useQuery hook', () => {
14
- it('Should render without failing', async () => {
13
+ describe('<DataQuery />', () => {
14
+ it('should render without failing', async () => {
15
15
  const data = {
16
16
  answer: 42
17
17
  };
@@ -22,18 +22,8 @@ describe('Testing custom data provider and useQuery hook', () => {
22
22
  data: data
23
23
  }, children);
24
24
 
25
- const renderFunction = jest.fn(({
26
- loading,
27
- error,
28
- data
29
- }) => {
30
- if (loading) return 'loading';
31
- if (error) return /*#__PURE__*/React.createElement("div", null, "error: ", error.message);
32
- return /*#__PURE__*/React.createElement("div", null, "data: ", data && data.answer);
33
- });
34
- const {
35
- getByText
36
- } = (0, _react.render)( /*#__PURE__*/React.createElement(_react3.DataQuery, {
25
+ const renderFunction = jest.fn(() => null);
26
+ (0, _react.render)( /*#__PURE__*/React.createElement(_react3.DataQuery, {
37
27
  query: {
38
28
  answer: {
39
29
  resource: 'answer'
@@ -42,24 +32,21 @@ describe('Testing custom data provider and useQuery hook', () => {
42
32
  }, renderFunction), {
43
33
  wrapper
44
34
  });
45
- expect(getByText(/loading/i)).not.toBeUndefined();
46
35
  expect(renderFunction).toHaveBeenCalledTimes(1);
47
36
  expect(renderFunction).toHaveBeenLastCalledWith(expect.objectContaining({
48
37
  called: true,
49
38
  loading: true
50
39
  }));
51
40
  await (0, _react.waitFor)(() => {
52
- getByText(/data: /i);
41
+ expect(renderFunction).toHaveBeenCalledTimes(2);
42
+ expect(renderFunction).toHaveBeenLastCalledWith(expect.objectContaining({
43
+ called: true,
44
+ loading: false,
45
+ data
46
+ }));
53
47
  });
54
- expect(getByText(/data: /i)).toHaveTextContent("data: ".concat(data.answer));
55
- expect(renderFunction).toHaveBeenCalledTimes(2);
56
- expect(renderFunction).toHaveBeenLastCalledWith(expect.objectContaining({
57
- called: true,
58
- loading: false,
59
- data
60
- }));
61
48
  });
62
- it('Should render an error', async () => {
49
+ it('should render an error', async () => {
63
50
  const expectedError = new Error('Something went wrong');
64
51
  const data = {
65
52
  test: () => {
@@ -73,18 +60,8 @@ describe('Testing custom data provider and useQuery hook', () => {
73
60
  data: data
74
61
  }, children);
75
62
 
76
- const renderFunction = jest.fn(({
77
- loading,
78
- error,
79
- data
80
- }) => {
81
- if (loading) return 'loading';
82
- if (error) return /*#__PURE__*/React.createElement("div", null, "error: ", error.message);
83
- return /*#__PURE__*/React.createElement("div", null, "data: ", data && data.test);
84
- });
85
- const {
86
- getByText
87
- } = (0, _react.render)( /*#__PURE__*/React.createElement(_react3.DataQuery, {
63
+ const renderFunction = jest.fn(() => null);
64
+ (0, _react.render)( /*#__PURE__*/React.createElement(_react3.DataQuery, {
88
65
  query: {
89
66
  test: {
90
67
  resource: 'test'
@@ -93,21 +70,18 @@ describe('Testing custom data provider and useQuery hook', () => {
93
70
  }, renderFunction), {
94
71
  wrapper
95
72
  });
96
- expect(getByText(/loading/i)).not.toBeUndefined();
97
73
  expect(renderFunction).toHaveBeenCalledTimes(1);
98
74
  expect(renderFunction).toHaveBeenLastCalledWith(expect.objectContaining({
99
75
  called: true,
100
76
  loading: true
101
77
  }));
102
78
  await (0, _react.waitFor)(() => {
103
- getByText(/error: /i);
79
+ expect(renderFunction).toHaveBeenCalledTimes(2);
80
+ expect(renderFunction).toHaveBeenLastCalledWith(expect.objectContaining({
81
+ called: true,
82
+ loading: false,
83
+ error: expectedError
84
+ }));
104
85
  });
105
- expect(renderFunction).toHaveBeenCalledTimes(2);
106
- expect(getByText(/error: /i)).toHaveTextContent("error: ".concat(expectedError.message));
107
- expect(renderFunction).toHaveBeenLastCalledWith(expect.objectContaining({
108
- called: true,
109
- loading: false,
110
- error: expectedError
111
- }));
112
86
  });
113
87
  });
@@ -2,82 +2,73 @@
2
2
 
3
3
  var _react = require("@testing-library/react");
4
4
 
5
- var _react2 = _interopRequireDefault(require("react"));
5
+ var React = _interopRequireWildcard(require("react"));
6
6
 
7
7
  var _react3 = require("../react");
8
8
 
9
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
9
+ function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
10
10
 
11
- const mockBackend = {
12
- target: jest.fn((type, query) => {
13
- expect(query.resource).toBe('target');
14
- expect(type).toBe('create');
15
- expect(query.data).toMatchObject({
16
- question: '?'
17
- });
18
- return Promise.resolve({
19
- answer: 42
20
- });
21
- })
22
- };
23
- describe('Test mutations', () => {
24
- it('Should call the mock callback', async () => {
25
- let doMutation;
26
- const renderFunction = jest.fn(([mutate, {
27
- called,
28
- loading,
29
- error,
30
- data
31
- }]) => {
32
- doMutation = mutate;
33
- if (!called) return 'uncalled';
34
- if (loading) return 'loading';
35
- if (error) return /*#__PURE__*/_react2.default.createElement("div", null, "error: ", error.message);
36
- if (data) return /*#__PURE__*/_react2.default.createElement("div", null, "data: ", data.answer);
37
- });
38
- const testMutation = {
39
- resource: 'target',
11
+ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
12
+
13
+ describe('<DataMutation />', () => {
14
+ it('should render without failing', async () => {
15
+ const endpointSpy = jest.fn(() => Promise.resolve(42));
16
+ const mutation = {
17
+ resource: 'answer',
40
18
  type: 'create',
41
19
  data: {
42
20
  question: '?'
43
21
  }
44
22
  };
45
- const {
46
- getByText
47
- } = (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_react3.CustomDataProvider, {
48
- data: mockBackend
49
- }, /*#__PURE__*/_react2.default.createElement(_react3.DataMutation, {
50
- mutation: testMutation
51
- }, renderFunction)));
52
- expect(getByText(/uncalled/i)).not.toBeUndefined();
53
- expect(renderFunction).toHaveBeenCalledTimes(1);
54
- expect(mockBackend.target).not.toHaveBeenCalled();
55
- expect(renderFunction).toHaveBeenLastCalledWith([expect.any(Function), {
23
+ const data = {
24
+ answer: endpointSpy
25
+ };
26
+
27
+ const wrapper = ({
28
+ children
29
+ }) => /*#__PURE__*/React.createElement(_react3.CustomDataProvider, {
30
+ data: data
31
+ }, children);
32
+
33
+ const renderSpy = jest.fn(() => null);
34
+ (0, _react.render)( /*#__PURE__*/React.createElement(_react3.DataMutation, {
35
+ mutation: mutation
36
+ }, renderSpy), {
37
+ wrapper
38
+ });
39
+ expect(endpointSpy).toHaveBeenCalledTimes(0);
40
+ expect(renderSpy).toHaveBeenCalledTimes(1);
41
+ expect(renderSpy).toHaveBeenLastCalledWith([expect.any(Function), expect.objectContaining({
56
42
  called: false,
57
43
  loading: false,
58
44
  engine: expect.any(Object)
59
- }]);
60
- expect(doMutation).not.toBeUndefined();
61
- (0, _react.act)(() => {
62
- doMutation && doMutation();
45
+ })]);
46
+ await (0, _react.act)(async () => {
47
+ const firstRenderSpyCall = renderSpy.mock.calls[0];
48
+ const firstRenderSpyArgument = firstRenderSpyCall[0];
49
+ const [mutate] = firstRenderSpyArgument;
50
+ await mutate();
51
+ });
52
+ (0, _react.waitFor)(() => {
53
+ expect(endpointSpy).toHaveBeenCalledTimes(1);
54
+ expect(renderSpy).toHaveBeenCalledTimes(2);
55
+ expect(renderSpy).toHaveBeenLastCalledWith([expect.any(Function), expect.objectContaining({
56
+ called: true,
57
+ loading: true,
58
+ engine: expect.any(Object)
59
+ })]);
60
+ });
61
+ (0, _react.waitFor)(() => {
62
+ expect(endpointSpy).toHaveBeenCalledTimes(1);
63
+ expect(renderSpy).toHaveBeenCalledTimes(3);
64
+ expect(renderSpy).toHaveBeenLastCalledWith([expect.any(Function), expect.objectContaining({
65
+ called: true,
66
+ loading: false,
67
+ data: {
68
+ answer: 42
69
+ },
70
+ engine: expect.any(Object)
71
+ })]);
63
72
  });
64
- expect(renderFunction).toHaveBeenCalledTimes(2);
65
- expect(renderFunction).toHaveBeenLastCalledWith([doMutation, {
66
- called: true,
67
- loading: true,
68
- engine: expect.any(Object)
69
- }]);
70
- expect(mockBackend.target).toHaveBeenCalledTimes(1);
71
- await (0, _react.waitFor)(() => getByText(/data: /i));
72
- expect(renderFunction).toHaveBeenCalledTimes(3);
73
- expect(renderFunction).toHaveBeenLastCalledWith([doMutation, {
74
- called: true,
75
- loading: false,
76
- data: {
77
- answer: 42
78
- },
79
- engine: expect.any(Object)
80
- }]);
81
- expect(getByText(/data: /i)).toHaveTextContent("data: 42");
82
73
  });
83
74
  });
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.isMetadataPackageInstallation = exports.addOrUpdateConfigurationProperty = exports.isAddOrUpdateSystemOrUserSetting = exports.isInterpretationCommentUpdate = exports.isCommentOnInterpretation = exports.isCreateOrUpdateInterpretation = exports.isCreateFeedbackMessage = exports.isReplyToMessageConversation = void 0;
6
+ exports.isMetadataPackageInstallation = exports.addOrUpdateConfigurationProperty = exports.isAddOrUpdateSystemOrUserSetting = exports.isInterpretationCommentUpdate = exports.isCommentOnInterpretation = exports.isUpdateInterpretation = exports.isCreateInterpretation = exports.isCreateFeedbackMessage = exports.isReplyToMessageConversation = void 0;
7
7
 
8
8
  /*
9
9
  * Requests that expect a "text/plain" Content-Type have been collected by scanning
@@ -28,33 +28,43 @@ exports.isReplyToMessageConversation = isReplyToMessageConversation;
28
28
 
29
29
  const isCreateFeedbackMessage = (type, {
30
30
  resource
31
- }) => type === 'create' && resource === 'messageConversations/feedback'; // POST or PUT to `interpretations/${objectType}/${id}` (add or update an interpretation)
31
+ }) => type === 'create' && resource === 'messageConversations/feedback'; // POST `interpretations/${objectType}/${id}` (add an interpretation to a visualization)
32
32
 
33
33
 
34
34
  exports.isCreateFeedbackMessage = isCreateFeedbackMessage;
35
35
 
36
- const isCreateOrUpdateInterpretation = (type, {
36
+ const isCreateInterpretation = (type, {
37
+ resource
38
+ }) => {
39
+ const pattern = /^interpretations\/(?:reportTable|chart|visualization|map|eventVisualization|eventReport|eventChart|dataSetReport)\/[a-zA-Z0-9]{11}$/;
40
+ return type === 'create' && pattern.test(resource);
41
+ }; // PUT to `interpretations/${id}` (update an interpretation)
42
+
43
+
44
+ exports.isCreateInterpretation = isCreateInterpretation;
45
+
46
+ const isUpdateInterpretation = (type, {
37
47
  resource,
38
48
  id
39
49
  }) => {
40
- if (type !== 'create' && type !== 'replace') {
50
+ if (type !== 'replace') {
41
51
  return false;
42
52
  }
43
53
 
44
54
  let resourcePattern;
45
55
 
46
- if (type === 'replace' && id) {
47
- resourcePattern = /^interpretations\/(?:reportTable|chart|visualization|map|eventReport|eventChart|dataSetReport)$/;
56
+ if (id) {
57
+ resourcePattern = /^interpretations$/;
48
58
  const idPattern = /^[a-zA-Z0-9]{11}$/;
49
59
  return resourcePattern.test(resource) && idPattern.test(id);
50
60
  }
51
61
 
52
- resourcePattern = /^interpretations\/(?:reportTable|chart|visualization|map|eventReport|eventChart|dataSetReport)\/[a-zA-Z0-9]{11}$/;
62
+ resourcePattern = /^interpretations\/[a-zA-Z0-9]{11}$/;
53
63
  return resourcePattern.test(resource);
54
64
  }; // POST to `interpretations/${id}/comments` (comment on an interpretation)
55
65
 
56
66
 
57
- exports.isCreateOrUpdateInterpretation = isCreateOrUpdateInterpretation;
67
+ exports.isUpdateInterpretation = isUpdateInterpretation;
58
68
 
59
69
  const isCommentOnInterpretation = (type, {
60
70
  resource
@@ -26,30 +26,58 @@ describe('isCreateFeedbackMessage', () => {
26
26
  })).toEqual(false);
27
27
  });
28
28
  });
29
- describe('isCreateOrUpdateInterpretation', () => {
29
+ describe('isCreateInterpretation', () => {
30
30
  it('returns true for a POST to "interpretations/chart/${id}"', () => {
31
- expect((0, _textPlainMatchers.isCreateOrUpdateInterpretation)('create', {
31
+ expect((0, _textPlainMatchers.isCreateInterpretation)('create', {
32
32
  resource: 'interpretations/chart/oXD88WWSQpR'
33
33
  })).toEqual(true);
34
34
  });
35
- it('returns true for a PUT to "interpretations/chart/${id}"', () => {
36
- expect((0, _textPlainMatchers.isCreateOrUpdateInterpretation)('replace', {
35
+ it('returns false for a PUT to "interpretations/chart/${id}"', () => {
36
+ expect((0, _textPlainMatchers.isCreateInterpretation)('replace', {
37
37
  resource: 'interpretations/chart/oXD88WWSQpR'
38
+ })).toEqual(false);
39
+ });
40
+ it('retuns false for PATCH requests with a valid query', () => {
41
+ expect((0, _textPlainMatchers.isCreateInterpretation)('update', {
42
+ resource: 'interpretations/chart/oXD88WWSQpR'
43
+ })).toEqual(false);
44
+ });
45
+ it('returns false for a request to a different resource', () => {
46
+ expect((0, _textPlainMatchers.isCreateInterpretation)('create', {
47
+ resource: 'interpretations/dummy/oXD88WWSQpR'
48
+ })).toEqual(false);
49
+ });
50
+ });
51
+ describe('isUpdateInterpretation', () => {
52
+ it('returns true for a PUT to "interpretations/${id}"', () => {
53
+ expect((0, _textPlainMatchers.isUpdateInterpretation)('replace', {
54
+ resource: 'interpretations/oXD88WWSQpR'
38
55
  })).toEqual(true);
39
56
  });
40
57
  it('returns true for PUT with populated query.id', () => {
41
- expect((0, _textPlainMatchers.isCreateOrUpdateInterpretation)('replace', {
42
- resource: 'interpretations/chart',
58
+ expect((0, _textPlainMatchers.isUpdateInterpretation)('replace', {
59
+ resource: 'interpretations',
43
60
  id: 'oXD88WWSQpR'
44
61
  })).toEqual(true);
45
62
  });
46
- it('retuns false for PATCH requests with a valid query', () => {
47
- expect((0, _textPlainMatchers.isCreateOrUpdateInterpretation)('update', {
48
- resource: 'interpretations/chart/oXD88WWSQpR'
63
+ it('returns false for a POST to "interpretations/${id}"', () => {
64
+ expect((0, _textPlainMatchers.isUpdateInterpretation)('create', {
65
+ resource: 'interpretations/oXD88WWSQpR'
66
+ })).toEqual(false);
67
+ });
68
+ it('returns false for a PATCH to "interpretations/${id}"', () => {
69
+ expect((0, _textPlainMatchers.isUpdateInterpretation)('update', {
70
+ resource: 'interpretations/oXD88WWSQpR'
71
+ })).toEqual(false);
72
+ });
73
+ it('returns false for PATCH with populated query.id', () => {
74
+ expect((0, _textPlainMatchers.isUpdateInterpretation)('update', {
75
+ resource: 'interpretations',
76
+ id: 'oXD88WWSQpR'
49
77
  })).toEqual(false);
50
78
  });
51
79
  it('returns false for a request to a different resource', () => {
52
- expect((0, _textPlainMatchers.isCreateOrUpdateInterpretation)('create', {
80
+ expect((0, _textPlainMatchers.isUpdateInterpretation)('create', {
53
81
  resource: 'interpretations/dummy/oXD88WWSQpR'
54
82
  })).toEqual(false);
55
83
  });