-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add tests for required data logic (#2334)
* 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
Showing
3 changed files
with
197 additions
and
1 deletion.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
dev-client/__tests__/integration/EditSiteNoteScreen.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
102
dev-client/__tests__/integration/RestrictByRequirements.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters