-
Notifications
You must be signed in to change notification settings - Fork 0
HW#7_Khyzhniak #24
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
base: master
Are you sure you want to change the base?
HW#7_Khyzhniak #24
Conversation
HW7(Tests)/Functions.js
Outdated
| async function getUsersEmails() { | ||
| const users = await getUsers(); | ||
| return users.map(user => user.email); | ||
| } |
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 better to put the logic for receiving an email into a separate clean function.
it will be easier to maintain and test.
and sequentially call a function to obtain general information on users and a function to transform / filter this data.
HW7(Tests)/Functions.test.js
Outdated
| /* | ||
| // IN CASE WE WANT TO TEST RESPONSE FROM OUR API | ||
|
|
||
| // NOTE! THAT`S NOT FOR OUT UNIT-TESTS | ||
|
|
||
| describe('Testing API respone with async function getUsers()', () => { | ||
| let users; | ||
|
|
||
| beforeAll(async () => { | ||
| users = await ( | ||
| await fetch('https://jsonplaceholder.typicode.com/users') | ||
| ).json(); | ||
| }); | ||
|
|
||
| test('Returned value is an Array and it`s length > 0', () => { | ||
| expect(Array.isArray(users)).toBe(true); | ||
| expect(users.length).toBeGreaterThan(0); | ||
| }); | ||
|
|
||
| test('Returned array contains objects with valid properties', async () => { | ||
| for (const user of users) { | ||
| expect(user).toMatchObject({ | ||
| username: expect.any(String), | ||
| name: expect.any(String), | ||
| id: expect.any(Number), | ||
| }); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| // WE SHOULD USE MOCKS INSTEAD, AS SHOWN BELOW | ||
| */ |
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.
if it code "NOT FOR OUT UNIT-TESTS" why we store it in this file?
HW7(Tests)/Functions.test.js
Outdated
| const invalidEmailsToCheck = [ | ||
| 'exmp@gmail.ru', | ||
| 'exm@mail.ru', | ||
| 'example @gmail.com', | ||
| 'example@gmail. com', | ||
| 'example@mailru', | ||
| 'exmple@mailru.', | ||
| '.example@mailru', | ||
| 'exm@.mailru', | ||
| 'exm@.', | ||
| 'examplemail.ua', | ||
| '@examplemail.ua', | ||
| 'examplemail.ua@', | ||
| 'example@12gmail.com', | ||
| 'example@gmail.12', | ||
| 'ex!ample@gmail.com', | ||
| 'ex?ample@gmail.com', | ||
| 'ex,ample@gmail.com', | ||
| 'ex@..a@mple@gmail.com', | ||
| 'ex.ample@gmail.com' | ||
| ]; |
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.
not critical, but for convenience, it is better to store data like this separately from the test itself. for example at the beginning of a file.
| const user = new User('Dmytro', 'Khyzhniak'); | ||
|
|
||
| test('Returned object properties and values are correct.', () => { | ||
| expect(user).toEqual({ | ||
| firstName: 'Dmytro', | ||
| lastName: 'Khyzhniak' | ||
| }); | ||
| }); | ||
|
|
||
| test('Method getFullName() returns correct result.', () => { | ||
| expect(user.getFullName()).toBe('Dmytro Khyzhniak'); |
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.
better store name in common constant
HW7(Tests)/Functions.test.js
Outdated
| describe('Passing only one argument to the User constructor.', () => { | ||
| const user = new User('Dmytro'); | ||
|
|
||
| test('Returned object properties and values are correct.', () => { | ||
| expect(user).toEqual({ | ||
| firstName: 'Dmytro', | ||
| lastName: 'Simpson' | ||
| }); | ||
| }); | ||
|
|
||
| test('Method getFullName() returns correct result.', () => { | ||
| expect(user.getFullName()).toBe('Dmytro Simpson'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Passing no arguments to the User constructor.', () => { | ||
| const user = new User(); | ||
|
|
||
| test('Returned object properties and values are correct.', () => { | ||
| expect(user).toEqual({ | ||
| firstName: 'Homer', | ||
| lastName: 'Simpson' | ||
| }); | ||
| }); | ||
|
|
||
| test('Method getFullName() returns correct result.', () => { | ||
| expect(user.getFullName()).toBe('Homer Simpson'); | ||
| }); | ||
| }); |
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.
in the test description it is better to add that we check the default values, so it will be clearer when you read the test without opening the source file
HW7(Tests)/Async-functions.js
Outdated
| @@ -0,0 +1,5 @@ | |||
| async function getUsers() { | |||
| return await (await fetch('https://jsonplaceholder.typicode.com/users')).json(); | |||
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.
why you need await twice?
| @@ -0,0 +1,51 @@ | |||
| // Using a mock to prevent accessing third-party API | |||
| function getUsers() { | |||
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 create mock asynchronous function as a synchronous one, it is wrong.
- it is better to indicate in the name of the file that it is a mock, otherwise it is easy to get confused in spoils. eg Async-functions.mock.js
- if you want to mock for a third-party function, then you need to mock exactly for - fetch('https://jsonplaceholder.typicode.com/users')
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.
- Fixed.
- Fully agree with you. But as I understood, the names of the "Async-functions.js" in the working folder and in the "__ mocks __" folder must be the same. If I will change the name of the mock then I will need to change the name of the original file and that`s not worth it.
P.S. I could be wrong - Fixed.
No description provided.