-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unit test for mobile draft #8363
base: main
Are you sure you want to change the base?
Conversation
b078a6b
to
48d3595
Compare
d91e8a1
to
6dfa62c
Compare
…component/index.tsx
508f499
to
b42ce63
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work!
Nothing blocking, but a couple of things that can be improved.
jest.mock('@components/formatted_text', () => jest.fn(() => null)); | ||
jest.mock('@components/formatted_time', () => jest.fn(() => null)); | ||
jest.mock('@components/compass_icon', () => jest.fn(() => null)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any strong reason to mock these three components?
import type {Database} from '@nozbe/watermelondb'; | ||
import type DraftModel from '@typings/database/models/servers/draft'; | ||
|
||
describe('Draft Post', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if it makes sense to add more tests here apart of the snapshot one.
layoutWidth: 100, | ||
location: 'draft', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see any tests (here or in other files) testing for what these props control. 0/5 if it makes sense to add such tests.
(useTheme as jest.Mock).mockReturnValue(mockTheme); | ||
(getUserTimezone as jest.Mock).mockReturnValue('UTC'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use jest.mocked
to avoid the casting, and keep the typings.
(useTheme as jest.Mock).mockReturnValue(mockTheme); | |
(getUserTimezone as jest.Mock).mockReturnValue('UTC'); | |
jest.mocked(useTheme).mockReturnValue(mockTheme); | |
jest.mocked(getUserTimezone).mockReturnValue('UTC'); |
If you are going to use these too often, you may want to just create something like...
const mockedUseTheme = jest.mocked(useTheme);
describe(..., () => {
...
beforeEach(() => {
mockedUseTheme.mockReturnValue(mockTheme);
...
const baseProps = { | ||
channel: {type: General.DM_CHANNEL, displayName: 'Direct Message Channel'} as ChannelModel, | ||
draftReceiverUser: undefined, | ||
updateAt: 1633024800000, | ||
rootId: undefined, | ||
testID: 'channel-info', | ||
currentUser: {timezone: 'UTC'} as unknown as UserModel, | ||
isMilitaryTime: true, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could have the base props predefined (be it in a function or an object) and just set the "relevant props for the test". For example...
function getBaseProps() {
return {
channel: {type: General.DM_CHANNEL, displayName: 'Direct Message Channel'} as ChannelModel,
draftReceiverUser: undefined,
updateAt: 1633024800000,
rootId: undefined,
testID: 'channel-info',
currentUser: {timezone: 'UTC'} as unknown as UserModel,
isMilitaryTime: true,
};
}
...
it(..., () => {
const props = getBaseProps();
props.channel = {type: General.DM_CHANNEL, displayName: 'Direct Message Channel'} as ChannelModel,
const wrapper = render(<DraftPostHeader {...props}/>);
....
Or if you prefer the object route...
const baseProps = {
...
};
...
it(..., () => {
const props = {
...baseProps,
channel: {type: General.DM_CHANNEL, displayName: 'Direct Message Channel'} as ChannelModel,
}
const wrapper = render(<DraftPostHeader {...props}/>);
...
expect(wrapper.toJSON()).toMatchSnapshot(); | ||
}); | ||
|
||
it('calls draftDeleteHandler when pressed', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we are only checking dismissBottomSheet
, but nothing else. Shouldn't we check that deleteDraftConfirmation
is called with the correct arguments?
const server = await TestHelper.setupServerDatabase(); | ||
database = server.database; | ||
}); | ||
it('should render the draft options component', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is hard to see the difference between the two tests.
expect(wrapper.toJSON()).toMatchSnapshot(); | ||
}); | ||
|
||
it('Should call editHandler when pressed', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment about this only checking for dismissBottomSheet
.
expect(wrapper.toJSON()).toMatchSnapshot(); | ||
}); | ||
|
||
it('should call dismissBottmSheet after sending the draft', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment about this only checking for dismissBottomSheet.
expect(wrapper.toJSON()).toMatchSnapshot(); | ||
}); | ||
|
||
it('should call dismissBottmSheet after sending the draft', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it('should call dismissBottmSheet after sending the draft', () => { | |
it('should call dismissBottomSheet after sending the draft', () => { |
Summary
Added unit test for mobile in draft PR: #8280
Ticket Link
https://mattermost.atlassian.net/browse/MM-61735
Checklist
E2E iOS tests for PR
.Release Note