-
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
Added rest client and local/remote/WebSocket handling for Scheduled Post #8526
base: feature_schedule_posts
Are you sure you want to change the base?
Conversation
@Rajat-Dabade it would be beneficial to write unit test as we need to hit 80% of code coverage by the end of January. |
b3ba535
to
aeecc3d
Compare
@rahimrahman I have added a test, can you please review this PR? Thank you. |
aaba87d
to
98ef776
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.
There is a couple of things I would like moved around, but apart of that it looks great.
await DatabaseManager.destroyServerDatabase(serverUrl); | ||
}); | ||
|
||
describe('handleScheduledPosts', () => { |
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 would prefer to have a describe for each function.
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.
Done
app/actions/local/scheduled_post.ts
Outdated
} | ||
} | ||
|
||
export async function handleCreateOrUpdateScheduledPost(serverUrl: string, msg: WebSocketMessage<ScheduledPostWebsocketEventPayload>, prepareRecordsOnly = false) { |
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.
This feels off. If this handles the websocket message, it should be in the websocket folder. If it is a generic local function (to be used by the websocket and the remote actions, for example), it should be agnostic to the websocket message (and for sure, not do the parsing).
0/5 on where this should be.
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 initially referred to channelBookmarks, but after reviewing other files, I found your point valid. I've now moved it to the WebSocket folder.
const mockScheduledPost = { | ||
...scheduledPosts[0], | ||
toApi: jest.fn().mockResolvedValue(scheduledPosts[0]), // Mock `toApi` | ||
}; | ||
jest.spyOn(database, 'get').mockImplementation(() => ({ | ||
query: jest.fn().mockReturnValue({ | ||
fetch: jest.fn().mockResolvedValue([mockScheduledPost]), | ||
}), | ||
}) as any); |
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.
Instead of mocking all this, you can just add the element to the database, and see if it gets deleted.
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.
Done.
await DatabaseManager.destroyServerDatabase(serverUrl); | ||
}); | ||
|
||
describe('fetch and delete scheduled posts', () => { |
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.
Use one describe for each function. That way the name of the test also can be simplified without so much repetition.
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.
Updated the file.
6bece54
to
9d62952
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.
Excellent work adding your tests.
There's a few patterns that I thought was unnecessary and repeated multiple times.
You can check your coverage and add the output as markdown as a screenshot so you know you coverage is above 80%. I think there's a guidance of that somewhere (I'll look for it)
expect(result).toBeDefined(); | ||
expect(result.error).toBeTruthy(); |
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.
Should you need to check result.toBeDefined(), when (result.error).toBeTrurthy()?
Would it be beneficial to know if what result.error.toEqual(something) instead of toBeTruthy()?
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.
Done.
expect(result).toBeDefined(); | ||
expect(result.error).toBeTruthy(); |
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 as above and in other test cases in this file. Knowing that the result.xyz is equal to something pretty much (result).toBeDefined().
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.
Done
describe('handleCreateOrUpdateSchedulePost', () => { | ||
it('handleCreateOrUpdateScheduledPost - handle not found database', async () => { | ||
const {error} = await handleCreateOrUpdateScheduledPost('foo', {} as WebSocketMessage) as {error: unknown}; | ||
expect(error).toBeTruthy(); |
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.
Does the error message says "Database not found?"
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.
Yup, updated the test case.
expect(deletedRecord).toBeTruthy(); | ||
expect(deletedRecord.models).toBeTruthy(); |
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.
Are these beneficial as models.length is asserted to 1?
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.
No, as such, removed it and updated the test.
expect(models).toBeTruthy(); | ||
expect(models?.length).toBe(1); |
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.
Might be more beneficials to know what the models that came back to equal to or a few elements matches the scheduled_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.
Done.
|
||
await client.getScheduledPostsForTeam(teamId, includeDirectChannels, groupLabel); | ||
|
||
expect(client.doFetch).toHaveBeenCalledWith(expectedUrl, expectedOptions); |
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.
Excellent!
|
||
it('fetch Schedule post - handle scheduled post enabled', async () => { | ||
mockedGetConfigValue.mockResolvedValueOnce('true'); | ||
jest.spyOn(operator, 'handleScheduledPosts').mockResolvedValueOnce([]); |
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.
Should you mock this or should you check that handleScheduledPosts() .toHaveBeenCalledWith() properly?
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.
Done.
import DatabaseManager from '@database/manager'; | ||
import {logError} from '@utils/log'; | ||
|
||
export async function handleScheduledPosts(serverUrl: string, actionType: string, scheduledPosts: ScheduledPost[], prepareRecordsOnly = false) { |
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.
Missing test?
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.
Added
}); | ||
|
||
it('delete Schedule post - handle scheduled post enabled', async () => { | ||
jest.spyOn(operator, 'handleScheduledPosts').mockResolvedValueOnce([]); |
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 as above, should we mock or check that it was calledWith() properly?
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.
Done.
const mockClient = { | ||
getScheduledPostsForTeam: jest.fn(() => Promise.resolve({...scheduledPostsResponse})), | ||
deleteScheduledPost: jest.fn((scheduledPostId) => { | ||
return Promise.resolve(scheduledPostsResponse.bar.find((post) => post.id === scheduledPostId)); |
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.
Curious: when you send deleteScheduledPost, you get the post that you deleted back?
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.
Yup, I know this is of no use, but the handler is written in this way (as we batch all the records for create/update/delete actions together and return Promise of Scheduled posts). It causes no harm but comes with benefits to test which record was actually deleted.
9d62952
to
e045d63
Compare
e045d63
to
3b3c02a
Compare
Summary
This PR handles the rest clients, local/remote/WebSocket handling of scheduled posts.
Ticket Link
https://mattermost.atlassian.net/browse/MM-62763
Checklist
E2E iOS tests for PR
.Release Note