Exploring Test case (Part 2)

Hello there, I hope all is okay with you. As part of my task last week, I worked on writing a test case for the “Name Display” feature during the login process. Here is a brief summary of what I accomplished. After I already get helped from Peter, Peter show me to write test script well written without any errors.

Next, continue with this week task. For this week, I continue to explore on the test script. About the task, Peter showed me on how to use test timer with beforeEach and afterEach. By using describe function used to group together related test cases with a common name which is “Test Timer”. The beforeEach function is a Jest lifecycle hook that specifies setup step that should be executed before each test case in the test suite. For this case, it is using “jest.useFakeTimers” with the “advanceTimers” option set to true. This will allow Jest to replace the real timers in the environment with fake timers that can be controlled programmatically in the test cases. It also calls “jest.resetModules()” to reset any modules that may have been cached by Jest, ensuring a clean state before each test.

Furthermore, for the first test which is “Display user name if not logged in”. It renders component with props.navigation and props.route as props. It then uses the “getByTestId” function to get the component is equal to the expected string. Second test which is “Display correct user name when logged in”. It renders component with props.navigation and props.route as props using the render function. It then calls jest.advanceTimersByTime to advance the timers. It asserts that the props.children of the retrieve component is equal to the expected string “Welcome, ${displayName} !”.

Leave a Reply