Skip to content

Commit

Permalink
test: Add tests for required data logic (#2334)
Browse files Browse the repository at this point in the history
* test: Add unit tests for RestrictByRequirements

* test: Add integration test for EditSiteNoteScreen rendering

* test: Add navigation to tests

* chore: Add copyright notice

* fix: Wrap tests in describe()
  • Loading branch information
knipec authored Oct 31, 2024
1 parent 0e250b2 commit 7b45050
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 1 deletion.
83 changes: 83 additions & 0 deletions dev-client/__tests__/integration/EditSiteNoteScreen.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright © 2024 Technology Matters
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/

import {testState} from '@testing/integration/data';
import {render} from '@testing/integration/utils';

import {EditSiteNoteScreen} from 'terraso-mobile-client/screens/SiteNotesScreen/EditSiteNoteScreen';

const mockedNavigate = jest.fn();
jest.mock('terraso-mobile-client/navigation/hooks/useNavigation', () => {
const actualNav = jest.requireActual(
'terraso-mobile-client/navigation/hooks/useNavigation',
);
return {
...actualNav,
useNavigation: () => ({navigate: mockedNavigate}),
};
});

afterEach(() => {
mockedNavigate.mockClear();
});

describe('EditSiteNoteScreen', () => {
test('renders if data exists', () => {
const screen = render(<EditSiteNoteScreen noteId="note1" siteId="1" />, {
route: 'EDIT_SITE_NOTE',
initialState: testState,
});

expect(screen.getByText('Site Note')).toBeOnTheScreen();
expect(screen.getByDisplayValue('note 1 contents')).toBeOnTheScreen();
expect(mockedNavigate).toHaveBeenCalledTimes(0);
});

test('renders null if site missing', () => {
const screen = render(
<EditSiteNoteScreen noteId="note1" siteId="nonexistent-site" />,
{
route: 'EDIT_SITE_NOTE',
initialState: testState,
},
);

// Ideally would want to test that navigation worked, but I can't figure out how to do that
expect(screen.queryByText('Site Note')).toBeNull();
expect(screen.queryByText('note 1 contents')).toBeNull();
expect(mockedNavigate).toHaveBeenCalledTimes(1);
expect(mockedNavigate).toHaveBeenCalledWith('BOTTOM_TABS');
});

test('renders null if note missing', () => {
const screen = render(
<EditSiteNoteScreen noteId="nonexistent-note" siteId="1" />,
{
route: 'EDIT_SITE_NOTE',
initialState: testState,
},
);

expect(screen.queryByText('Site Note')).toBeNull();
expect(screen.queryByText('note 1 contents')).toBeNull();
expect(mockedNavigate).toHaveBeenCalledTimes(1);
expect(mockedNavigate).toHaveBeenCalledWith('SITE_TABS', {
initialTab: 'NOTES',
siteId: '1',
});
});
});
102 changes: 102 additions & 0 deletions dev-client/__tests__/integration/RestrictByRequirements.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright © 2024 Technology Matters
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/

import {Text} from 'react-native';

import {render} from '@testing/integration/utils';

import {RestrictByRequirements} from 'terraso-mobile-client/components/dataRequirements/RestrictByRequirements';

describe('RestrictByRequrements', () => {
test('renders children and triggers no actions when required data exists', () => {
let thingsDone = '';
const requirements = [
{
data: {dummyData: 1},
doIfMissing: () => (thingsDone += 'object1'),
},
{
// This is an object, so it exists despite having only undefined properties
data: {dummyData: undefined},
doIfMissing: () => (thingsDone += 'object2'),
},
];

const {queryByText} = render(
<RestrictByRequirements requirements={requirements}>
{() => <Text>Hello world</Text>}
</RestrictByRequirements>,
);

expect(queryByText('Hello world')).toBeTruthy();
expect(thingsDone).toEqual('');
});

test('does not render children and triggers action when earliest missing required data is undefined', () => {
let thingsDone = '';
const requirements = [
{
data: {dummyData: 1},
doIfMissing: () => (thingsDone += 'object'),
},
{
data: undefined,
doIfMissing: () => (thingsDone += 'undefined'),
},
{
data: null,
doIfMissing: () => (thingsDone += 'null'),
},
];

const {queryByText} = render(
<RestrictByRequirements requirements={requirements}>
{() => <Text>Hello world</Text>}
</RestrictByRequirements>,
);

expect(queryByText('Hello world')).toBeNull();
expect(thingsDone).toEqual('undefined');
});

test('does not render children and triggers action when earliest missing required data is null', () => {
let thingsDone = '';
const requirements = [
{
data: null,
doIfMissing: () => (thingsDone += 'null'),
},
{
data: undefined,
doIfMissing: () => (thingsDone += 'undefined'),
},
{
data: {undefined},
doIfMissing: () => (thingsDone += 'object'),
},
];

const {queryByText} = render(
<RestrictByRequirements requirements={requirements}>
{() => <Text>Hello world</Text>}
</RestrictByRequirements>,
);

expect(queryByText('Hello world')).toBeNull();
expect(thingsDone).toEqual('null');
});
});
13 changes: 12 additions & 1 deletion dev-client/jest/integration/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,18 @@ export const testState: Partial<AppState> = {
privacy: 'PRIVATE',
archived: false,
updatedAt: '',
notes: {},
notes: {
note1: {
id: 'note1',
siteId: '1',
content: 'note 1 contents',
createdAt: 'date created',
updatedAt: 'date updated',
authorId: '1',
authorFirstName: 'First',
authorLastName: 'Last',
},
},
projectId: '1',
},
},
Expand Down

0 comments on commit 7b45050

Please sign in to comment.