{"size": 2775, "ext": "js", "max_stars_repo_path": "src/node/app.test.js", "max_stars_repo_name": "Nikelin96/heroes_backend", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/node/app.test.js", "max_issues_repo_name": "Nikelin96/heroes_backend", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/node/app.test.js", "max_forks_repo_name": "Nikelin96/heroes_backend", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 25.6944444444, "max_line_length": 71, "alphanum_fraction": 0.5264864865}
const supertest = require("supertest");
const { getApp } = require('./app');

test("GET returns 200Ok", async () => {

    // arrange
    const heroes = [];
    const repositoryMock = {
        getHeroesAsync: jest.fn().mockImplementation(async () => {
            return heroes;
        })
    }
    app = getApp(repositoryMock);

    // act
    await supertest(app).get("/")
        // assert
        .expect(200)
        .then((response) => {
            expect(response.body).toStrictEqual(heroes);
        });
});

test("GET by name returns 200Ok", async () => {
    // arrange
    const heroName = 'name';
    const repositoryMock = {
        getHeroesAsync: jest.fn().mockImplementation(async () => {
            return [{ name: heroName }, { name: 'name2' }];
        })
    }
    app = getApp(repositoryMock);
    // act
    await supertest(app).get(`?searchText=${heroName}`)
        // assert
        .expect(200)
        .then((response) => {
            expect(response.body).toStrictEqual({ name: heroName });
        });
});

test("GET by id returns 200Ok", async () => {
    // arrange
    const heroId = 123;
    const repositoryMock = {
        getHeroesAsync: jest.fn().mockImplementation(async () => {
            return [{ id: 122 }, { id: heroId }, { id: 124 }];
        })
    }
    app = getApp(repositoryMock);
    // act
    await supertest(app).get(`/${heroId}`)
        // assert
        .expect(200)
        .then((response) => {
            expect(response.body).toStrictEqual({ id: heroId });
        });
});

test("POST returns 201Created", async () => {
    // arrange
    const payload = { name: 'name1' };
    const repositoryMock = {
        createHeroAsync: jest.fn().mockImplementation(async (hero) => {
            return hero;
        })
    }
    app = getApp(repositoryMock);
    // act
    await supertest(app).post(`/`)
        .send(payload)
        // assert
        .expect(201)
        .then((response) => {
            expect(response.body).toStrictEqual(payload);
        });
});

test("PUT returns 200Ok", async () => {
    // arrange
    const payload = { name: 'name1' };
    const repositoryMock = {
        updateHeroAsync: jest.fn().mockImplementation(async (hero) => {
            return hero;
        })
    }
    app = getApp(repositoryMock);
    // act
    await supertest(app).put(`/`)
        .send(payload)
        // assert
        .expect(200);
});

test("DELETE by id returns 200Ok", async () => {
    // arrange
    const heroId = 123;
    const repositoryMock = {
        deleteHeroAsync: jest.fn().mockImplementation(async (id) => {
            return id;
        })
    }
    app = getApp(repositoryMock);
    // act
    await supertest(app).delete(`/${heroId}`)
        // assert
        .expect(200)
});