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