diff --git a/src/__test__/DetailPengguna.test.js b/src/__test__/DetailPengguna.test.js index 995e3894b47f014e20e0996e3a56fd119583eb7b..58cc74fbf63af92c47cf4f8e671de221eb621da8 100644 --- a/src/__test__/DetailPengguna.test.js +++ b/src/__test__/DetailPengguna.test.js @@ -9,29 +9,44 @@ beforeEach(() => { afterEach(cleanup); test("Test detail pengguna renders", async () => { - fetch.mockResponseOnce(JSON.stringify({ - "id": "663392ac-1dd6-462b-9301-a19c1287cefd", - "username": "dummyuser", - "full_name": "Dummy User", - "phone_number": "+6285212345678", - "address": "Jl. Dummy No.1", - "neighborhood": "000", - "hamlet": "000", - "urban_village": "Dummy Urban Village", - "sub_district": "Dummy Sub-District", - "profile_picture": null - })); - const {getByTestId} = render(<AuthContext.Provider value={{profile:{token:"BEBAS"}}}>x<DetailPengguna /></AuthContext.Provider>); - await waitForDomChange(getByTestId("profile")); - const profile = getByTestId("profile"); - expect (profile.textContent).toContain("dummyuser"); - expect (profile.textContent).toContain("+6285212345678"); - expect (profile.textContent).toContain("Jl. Dummy No.1"); - expect (profile.textContent).toContain("000"); - expect (profile.textContent).toContain("000"); - expect (profile.textContent).toContain("Dummy Sub-District"); - expect (profile.textContent).toContain("Dummy Urban Village"); + fetch.mockResponseOnce( + JSON.stringify({ + id: "663392ac-1dd6-462b-9301-a19c1287cefd", + username: "dummyuser", + full_name: "Dummy User", + phone_number: "+6285212345678", + address: "Jl. Dummy No.1", + neighborhood: "000", + hamlet: "000", + urban_village: "Dummy Urban Village", + sub_district: "Dummy Sub-District", + profile_picture: null + }) + ); + const { getByTestId } = render( + <AuthContext.Provider value={{ profile: { token: "BEBAS" } }}> + <DetailPengguna /> + </AuthContext.Provider> + ); + await waitForDomChange(getByTestId("profile")); + const profile = getByTestId("profile"); + expect(profile.textContent).toContain("dummyuser"); + expect(profile.textContent).toContain("+6285212345678"); + expect(profile.textContent).toContain("Jl. Dummy No.1"); + expect(profile.textContent).toContain("000"); + expect(profile.textContent).toContain("000"); + expect(profile.textContent).toContain("Dummy Sub-District"); + expect(profile.textContent).toContain("Dummy Urban Village"); }); - - +test("Test mock return error", async () => { + fetch.mockReject(new Error("fake error message")); + const { getByTestId } = render( + <AuthContext.Provider value={{ profile: { token: "BEBAS" } }}> + <DetailPengguna /> + </AuthContext.Provider> + ); + await waitForDomChange(getByTestId("page")); + const page = getByTestId("page"); + expect(page.textContent).toContain("Error !, Please relogin.."); +}); diff --git a/src/__test__/Login.test.js b/src/__test__/Login.test.js index 6c67ea5a7377baf92aff414b670d667c9b2358fa..69b00e509c0e26ae79d2af7c9a3f116e732eef39 100644 --- a/src/__test__/Login.test.js +++ b/src/__test__/Login.test.js @@ -3,7 +3,6 @@ import { act, cleanup, fireEvent, render } from "@testing-library/react"; import Login from "../page/Login"; import * as AuthContext from "../utils/contex"; - beforeEach(() => { fetch.resetMocks(); }); @@ -28,12 +27,11 @@ test("Test login page validation", async () => { test("Test login submitted", async () => { const handleLogin = jest.fn(); - fetch.mockResponseOnce(JSON.stringify({"token":"AAAAAA"})); + fetch.mockResponseOnce(JSON.stringify({ token: "AAAAAA" })); jest .spyOn(AuthContext, "useAuthContext") .mockImplementation(() => ({ is_authenticated: true, handleLogin })); - const { getByTestId } = render(<Login />); const username = getByTestId("name-input"); @@ -48,3 +46,22 @@ test("Test login submitted", async () => { expect(handleLogin).toBeCalled(); expect(fetch).toBeCalled(); }); + +test("Test login error", async () => { + fetch.mockResponseOnce([ + JSON.stringify([{ token: "AAAAAA" }]), + { status: 404 } + ]); + const { getByTestId } = render(<Login />); + const username = getByTestId("name-input"); + const password = getByTestId("password-input"); + await act(async () => { + await fireEvent.input(username, { target: { value: "test" } }); + await fireEvent.input(password, { target: { value: "password" } }); + }); + await act(async () => { + await fireEvent.submit(getByTestId("login")); + }); + const login = getByTestId("login"); + expect(login.textContent).toContain("Password salah !"); +}); diff --git a/src/__test__/table.test.js b/src/__test__/table.test.js new file mode 100644 index 0000000000000000000000000000000000000000..9688136297f200a9e63a4a3865a721d70908682d --- /dev/null +++ b/src/__test__/table.test.js @@ -0,0 +1,124 @@ +import React from "react"; +import { act, cleanup, fireEvent, render } from "@testing-library/react"; +import Table from "../component/table"; +import AuthContext from "../utils/contex"; + +beforeEach(() => { + fetch.resetMocks(); +}); +afterEach(cleanup); + +test("use search bar", async () => { + const data = { + url: `${process.env.REACT_APP_BASE_URL}/users/`, + pageDefault: 1, + pageSize: 7, + searchDefault: "", + pageNeighbours: 1, + title: "Pengguna", + keyValuePairs: [ + ["id", "id"], + ["full_name", "Nama Lengkap"], + ["username", "Username"], + ["dummy", "dummy"], + ["dummy", "dummy"] + ], + link: "/pengguna/details/" + }; + fetch.mockResponseOnce( + JSON.stringify({ + count: 6, + next: null, + previous: null, + results: [ + { + id: "45897cc5-968c-44cf-931d-e646b095fcaf", + username: "admin-staging", + full_name: "", + phone_number: "", + address: "", + neighborhood: "", + hamlet: "", + urban_village: "", + sub_district: "", + profile_picture: null + }, + { + id: "663392ac-1dd6-462b-9301-a19c1287cefd", + username: "dummyuser", + full_name: "Dummy User", + phone_number: "+6285212345678", + address: "Jl. Dummy No.1", + neighborhood: "000", + hamlet: "000", + urban_village: "Dummy Urban Village", + sub_district: "Dummy Sub-District", + profile_picture: null + }, + { + id: "eafaa6d5-cc28-42bd-8fa3-e6eeecbfb2e0", + username: "mpit", + full_name: "annisaa fitri shabrina", + phone_number: "+6282190772106", + address: "jalan taman ayun vi", + neighborhood: "004", + hamlet: "016", + urban_village: "pengglingan", + sub_district: "kecamatan", + profile_picture: null + }, + { + id: "50d94bea-c164-4722-b5c0-cc3fda9b6e9c", + username: "pidyo", + full_name: "pidypidy", + phone_number: "+628219864572", + address: "buaran", + neighborhood: "004", + hamlet: "006", + urban_village: "michael", + sub_district: "halim", + profile_picture: null + }, + { + id: "d4b98bb5-8ba4-4a41-af10-93abcf53df58", + username: "whtestest", + full_name: "Michael Wiryadinata", + phone_number: "+628192090199", + address: "Ada deh 123", + neighborhood: "001", + hamlet: "002", + urban_village: "ada deh", + sub_district: "mau tau", + profile_picture: + "https://industripilar-api-staging.s3.amazonaws.com/media/uploads/profile/cf3456e1-e4b2-4da9-a6c6-35c3a3a47cbc6842089823804677314.jpg" + }, + { + id: "1bcf4409-4894-4f80-97af-b9f9ce47f01e", + username: "whtestest2", + full_name: "michaeleh", + phone_number: "+628192090198", + address: "zjsjsn", + neighborhood: "012", + hamlet: "003", + urban_village: "nsjsns", + sub_district: "bhjsns", + profile_picture: null + } + ] + }) + ); + const { getByPlaceholderText, getByTestId } = render( + <AuthContext.Provider value={{ profile: { token: "BEBAS" } }}> + <Table {...data} /> + </AuthContext.Provider> + ); + const search = getByPlaceholderText("Search..."); + await act(async () => { + await fireEvent.input(search, { target: { value: "Dummy User" } }); + }); + await act(async () => { + await fireEvent.submit(getByPlaceholderText("Search...")); + }); + const table = getByTestId("table"); + expect(table.textContent).toContain("Dummy User"); +}); diff --git a/src/component/table.jsx b/src/component/table.jsx index 1202173a04c471ad8a4593a460ccde92c1c7c813..dd638eb379fc3861ee9b3088345c92c2b0274772 100644 --- a/src/component/table.jsx +++ b/src/component/table.jsx @@ -54,7 +54,8 @@ const Table = ({ `} > {errorState && <ErrorDiv>ERROR !! Please relogin.</ErrorDiv>} - <div data-testid="page" + <div + data-testid="page" css={css` font-style: normal; font-weight: normal; @@ -87,6 +88,7 @@ const Table = ({ </form> </div> <div + data-testid="table" css={css` margin-top: 1.5rem; `} diff --git a/src/page/DetailPengguna.jsx b/src/page/DetailPengguna.jsx index 45aa0da37f596d73e3c6196a08a92bdcccca557b..c60be2f200c33de1f980df479cdaa9b358eca6d6 100644 --- a/src/page/DetailPengguna.jsx +++ b/src/page/DetailPengguna.jsx @@ -14,7 +14,7 @@ const DetailPengguna = ({ userId }) => { const url = `${process.env.REACT_APP_BASE_URL}/users/${userId}/`; const [user, error] = useFetchSingleData(url); return ( - <div + <div data-testid="page" css={css` width: 100%; height: 100%;