Skip to content

Conversation

@dmytro-khyzhniak
Copy link
Collaborator

No description provided.

Comment on lines 18 to 21
async function getUsersEmails() {
const users = await getUsers();
return users.map(user => user.email);
}

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.

Comment on lines 104 to 135
/*
// 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
*/

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?

Comment on lines 27 to 47
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'
];

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.

Comment on lines +59 to +69
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');

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

Comment on lines 73 to 101
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');
});
});

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

@@ -0,0 +1,5 @@
async function getUsers() {
return await (await fetch('https://jsonplaceholder.typicode.com/users')).json();

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() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. you create mock asynchronous function as a synchronous one, it is wrong.
  2. 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
  3. if you want to mock for a third-party function, then you need to mock exactly for - fetch('https://jsonplaceholder.typicode.com/users')

Copy link
Collaborator Author

@dmytro-khyzhniak dmytro-khyzhniak Mar 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Fixed.
  2. 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
  3. Fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants