-
Notifications
You must be signed in to change notification settings - Fork 7
test: add partial challenge tests #1279
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
Open
igorntwari
wants to merge
25
commits into
dev
Choose a base branch
from
test/challenge-card
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 21 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
ba5cb3c
test: add partial challenge tests
igorntwari f867447
test: add full challenge tests
igorntwari 6db1bea
Merge branch 'dev' into test/challenge-card
igorntwari 53af166
fix: add missing data-tesid
igorntwari 025a803
fix: rename state variable
igorntwari 098d751
fix: node version
igorntwari aca13f7
fix: renamve node version
igorntwari 1290fea
fix: add clearclearImmediate
igorntwari 4d68ac9
fix: downgrade node version
igorntwari 1a58301
feat: add button test
igorntwari 2584ba8
fix: clearImediate function
igorntwari f982b0d
refactor: remove duplicate mocks
igorntwari 0fca9c8
refactor: remove extra duplicates
igorntwari 25f4082
refactor: remove unnecessary codes
igorntwari c92c11d
test: challenge card
igorntwari c408ff4
Merge branch 'dev' into test/challenge-card
igorntwari 0c24ca0
feat: install need packages
igorntwari dff4c97
feat: add more reliable test
igorntwari eee7016
refactor: remove deleted file
igorntwari c525fba
fix: remove deleted test files
igorntwari f76dfd0
Merge branch 'dev' into test/challenge-card
igorntwari 8a9bdb3
refactor: add proper props naming
igorntwari 27a6acf
refactor: add proper testid and fix linting issues
igorntwari f98e70d
Merge branch 'dev' into test/challenge-card
igorntwari 4adffb1
refactor: add proper type coerciaon
igorntwari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { screen } from "@testing-library/react"; | ||
import "@testing-library/jest-dom"; | ||
import Badges from "@/components/badges"; | ||
import { renderWithRedux } from "@__mocks__/renderWithRedux"; | ||
import { challenge } from "@__mocks__/fixtures/challenge"; | ||
|
||
describe("Badge-card", () => { | ||
it("should render the challenge level in challenge card", () => { | ||
renderWithRedux(<Badges />); | ||
const challengeLevel = screen.getByTestId("badge"); | ||
expect(challengeLevel).toBeInTheDocument(); | ||
}); | ||
it("should render the team challenge badge", () => { | ||
const teamChallenge = { ...challenge, level: 1, isTeamChallenge: true, isHackathon: false }; | ||
renderWithRedux(<Badges challenge={teamChallenge} />); | ||
const teamChallengeValue = screen.getByText("Team challenge"); | ||
expect(teamChallengeValue).toBeInTheDocument(); | ||
expect(teamChallengeValue).toHaveTextContent("Team challenge"); | ||
}); | ||
|
||
it("should render the Hackathon badge", () => { | ||
const hackathonChallenge = { ...challenge, level: 0, isTeamChallenge: true, isHackathon: true }; | ||
renderWithRedux(<Badges challenge={hackathonChallenge} />); | ||
const teamChallengeValue = screen.getByText("Hackathon challenge"); | ||
expect(teamChallengeValue).toBeInTheDocument(); | ||
expect(teamChallengeValue).toHaveTextContent("Hackathon challenge"); | ||
}); | ||
it('should display BEGINNER when the challenge level 0', () => { | ||
renderWithRedux(<Badges courseLevel={1} />); | ||
const beginnerTag = screen.getByText('course.challenge.level-0'); | ||
expect(beginnerTag).toBeInTheDocument(); | ||
expect(beginnerTag).toHaveTextContent('course.challenge.level-0') | ||
}); | ||
it('should display INTERMEDIATE when the challenge lever is 2',()=> { | ||
renderWithRedux(<Badges courseLevel={2} />); | ||
const intermediateTag = screen.getByText('course.challenge.level-2'); | ||
expect(intermediateTag).toBeInTheDocument(); | ||
expect(intermediateTag).toHaveTextContent('course.challenge.level-2') | ||
}) | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import ChallengeCard, { ChallengeCardProps } from "@/components/cards/challenge/Challenge"; | ||
import "@testing-library/jest-dom"; | ||
import { screen } from "@testing-library/react"; | ||
import { renderWithRedux } from "@__mocks__/renderWithRedux"; | ||
import { challenge } from "@__mocks__/fixtures/challenge"; | ||
import { mockCommunity } from "@__mocks__/fixtures/community"; | ||
|
||
|
||
const mockChallengeCardProps: ChallengeCardProps = { | ||
data: { | ||
...challenge, | ||
isHackathon: true, | ||
}, | ||
community: mockCommunity, | ||
isCourseEnd: true, | ||
}; | ||
|
||
describe("ChallengeCard", () => { | ||
it("should render the ChallengeCard", () => { | ||
renderWithRedux(<ChallengeCard {...mockChallengeCardProps} />); | ||
const challengeCard = screen.getByTestId("challenge-card"); | ||
expect(challengeCard).toBeInTheDocument(); | ||
}); | ||
|
||
it("should display the challenge name", () => { | ||
renderWithRedux(<ChallengeCard {...mockChallengeCardProps} />); | ||
const challengeName = screen.getByText(mockChallengeCardProps.data.name); | ||
expect(challengeName).toBeInTheDocument(); | ||
}); | ||
|
||
it("should display the challenge description", () => { | ||
renderWithRedux(<ChallengeCard {...mockChallengeCardProps} />); | ||
const description = screen.getByText(mockChallengeCardProps.data.description); | ||
expect(description).toBeInTheDocument(); | ||
}); | ||
|
||
it("should display the correct number of learning materials", () => { | ||
renderWithRedux(<ChallengeCard {...mockChallengeCardProps} />); | ||
const learningMaterialsText = screen.getByText( | ||
`${mockChallengeCardProps.data.learningModules.length + mockChallengeCardProps.data.courses.length} Learning materials included` | ||
); | ||
expect(learningMaterialsText).toBeInTheDocument(); | ||
}); | ||
|
||
it("should display the reward certificate", () => { | ||
renderWithRedux(<ChallengeCard {...mockChallengeCardProps} />); | ||
const rewardCertificate = screen.getByText("communities.overview.challenge.unlock.certificate"); | ||
expect(rewardCertificate).toBeInTheDocument(); | ||
}); | ||
|
||
it("should link to the correct challenge page", () => { | ||
renderWithRedux(<ChallengeCard {...mockChallengeCardProps} />); | ||
const link = screen.getByRole("link"); | ||
expect(link).toHaveAttribute("href", `/communities/${mockChallengeCardProps.community.slug}/challenges/${mockChallengeCardProps.data.id}`); | ||
}); | ||
it("should render the challenge image", () => { | ||
renderWithRedux(<ChallengeCard {...mockChallengeCardProps} />); | ||
const challengeImage = screen.getByAltText("achievement"); | ||
expect(challengeImage).toBeInTheDocument(); | ||
}); | ||
|
||
it("should display the reward certificate component with proper rewards", () => { | ||
renderWithRedux(<ChallengeCard {...mockChallengeCardProps} />); | ||
const rewardCertificateText = screen.getByText("communities.overview.challenge.unlock.certificate"); | ||
expect(rewardCertificateText).toBeInTheDocument(); | ||
}); | ||
it('should indicate if you participate in challenge hackathon',()=> { | ||
renderWithRedux(<ChallengeCard {...mockChallengeCardProps}/>) | ||
const hackathonText = screen.getByText('communities.overview.challenge.participate') | ||
expect(hackathonText).toBeInTheDocument() | ||
}) | ||
|
||
}); |
39 changes: 39 additions & 0 deletions
39
__tests__/components/cards/challenge/ConfirmTeamInvitation.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import ConfirmTeamInvitation, { ConfirmTeamInvitationProps } from "@/components/cards/challenge/ConfirmTeamInvitation"; | ||
import "@testing-library/jest-dom"; | ||
import { screen } from "@testing-library/react"; | ||
import { renderWithRedux } from "@__mocks__/renderWithRedux"; | ||
import { useDispatch, AppDispatch } from "@/hooks/useTypedDispatch"; | ||
import { mockInvite } from "@__mocks__/fixtures/challenge"; | ||
|
||
|
||
const mockConfirmTeamInvitation : ConfirmTeamInvitationProps = { | ||
index: 1, | ||
title: "ConfirmTeamInvitation", | ||
text: "welcome to our team", | ||
invite: mockInvite, | ||
} | ||
|
||
jest.mock("@/hooks/useTypedDispatch.ts", () => ({ | ||
useDispatch: jest.fn(), | ||
})); | ||
const dispatchMock = jest.fn() as jest.MockedFunction<AppDispatch>; | ||
const useDispatchMock = useDispatch as jest.MockedFunction<typeof useDispatch>; | ||
|
||
describe("ConfirmTeamInvitation", () => { | ||
beforeEach(() => { | ||
dispatchMock.mockClear(); | ||
useDispatchMock.mockReturnValue(dispatchMock); | ||
}); | ||
|
||
it("should render the ConfirmTeamInvitation component", () => { | ||
renderWithRedux(<ConfirmTeamInvitation {...mockConfirmTeamInvitation} />); | ||
const confirmTeamInvitation = screen.getByTestId("confirmTeamInvitation"); | ||
expect(confirmTeamInvitation).toBeInTheDocument(); | ||
}); | ||
|
||
it("should render the ReplyToInvitation component within ConfirmTeamInvitation", () => { | ||
renderWithRedux(<ConfirmTeamInvitation {...mockConfirmTeamInvitation} />); | ||
const replyToInvitation = screen.getByTestId("reply-to-invitation" ); | ||
expect(replyToInvitation).toBeInTheDocument(); | ||
}); | ||
}); |
47 changes: 47 additions & 0 deletions
47
__tests__/components/cards/challenge/_partials/Button.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import Button from "@/components/cards/challenge/_partials/Button"; | ||
import { renderWithRedux } from "@__mocks__/renderWithRedux"; | ||
import "@testing-library/jest-dom"; | ||
import { screen, fireEvent } from "@testing-library/react"; | ||
import { ButtonProps } from "@/components/cards/challenge/_partials/Button"; | ||
|
||
const buttonProps: ButtonProps = { | ||
text: "Test Button", | ||
onClick: jest.fn(), | ||
loading: true, | ||
buttonTestId: "button", | ||
}; | ||
|
||
function RenderButton(props: ButtonProps = buttonProps) { | ||
renderWithRedux(<Button text={props.text} onClick={props.onClick} loading={props.loading} buttonTestId={props.buttonTestId} />); | ||
return screen.getByTestId(buttonProps.buttonTestId!); | ||
} | ||
|
||
describe("Button", () => { | ||
it("should render the button", () => { | ||
const button = RenderButton(); | ||
expect(button).toBeInTheDocument(); | ||
}); | ||
|
||
it("should show loader when loading is true", () => { | ||
RenderButton({ ...buttonProps, loading: true }); | ||
expect(screen.getByTestId("loader")).toBeInTheDocument(); | ||
}); | ||
|
||
it("should not show loader when loading is false", () => { | ||
RenderButton({ ...buttonProps, loading: false }); | ||
expect(screen.queryByTestId("loader")).not.toBeInTheDocument(); | ||
}); | ||
|
||
it("should call onClick when clicked", () => { | ||
const onClickMock = jest.fn(); | ||
RenderButton({ ...buttonProps, onClick: onClickMock, loading: false }); | ||
fireEvent.click(screen.getByTestId(buttonProps.buttonTestId!)); | ||
expect(onClickMock).toHaveBeenCalled(); | ||
}); | ||
|
||
it("should display text when loading is false and isTextVisible is true", () => { | ||
renderWithRedux(<Button {...buttonProps} loading={false} />); | ||
fireEvent.mouseEnter(screen.getByTestId(buttonProps.buttonTestId!)); | ||
expect(screen.getByTestId("button-text")).toHaveTextContent(buttonProps.text); | ||
}); | ||
}); |
36 changes: 36 additions & 0 deletions
36
__tests__/components/cards/challenge/_partials/FormTeam.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import FormTeamCard, { FormTeamCardProps } from "@/components/cards/challenge/_partials/FormTeam"; | ||
import { renderWithRedux } from "@__mocks__/renderWithRedux"; | ||
import "@testing-library/jest-dom"; | ||
import { screen } from '@testing-library/react'; | ||
|
||
const formTeamCardProps: FormTeamCardProps = { | ||
title: "FormTeamCardProps", | ||
description: "this is FormTeamCardProps description", | ||
index: 1, | ||
}; | ||
|
||
function RenderFormTeamCard(props: FormTeamCardProps = formTeamCardProps) { | ||
renderWithRedux( | ||
<FormTeamCard | ||
title={props.title} | ||
description={props.description} | ||
index={props.index} | ||
/> | ||
); | ||
} | ||
|
||
describe("FormTeamCard", () => { | ||
it('should render the FormTeamCard', () => { | ||
RenderFormTeamCard(); | ||
expect(screen.getByText(formTeamCardProps.title)).toBeInTheDocument(); | ||
expect(screen.getByText(formTeamCardProps.description)).toBeInTheDocument(); | ||
expect(screen.getByText(`${formTeamCardProps.index}.`)).toBeInTheDocument(); | ||
}); | ||
|
||
it('should have a link to the correct URL', () => { | ||
RenderFormTeamCard(); | ||
const linkElement = screen.getByRole('link', { name: 'Start now' }); | ||
expect(linkElement).toHaveAttribute('href', 'https://t.me/+0oJye8IwAuxkMDY0'); | ||
expect(linkElement).toHaveAttribute('target', '_blank'); | ||
}); | ||
}); |
48 changes: 48 additions & 0 deletions
48
__tests__/components/cards/challenge/_partials/InvitationButtom.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import InvitationButton, { InvitationButtonProps } from "@/components/cards/challenge/_partials/InvitationButton"; | ||
import { renderWithRedux } from "@__mocks__/renderWithRedux"; | ||
import "@testing-library/jest-dom"; | ||
import { screen, fireEvent } from "@testing-library/react"; | ||
|
||
const invitationButtonPropsAccept: InvitationButtonProps = { | ||
text: "accept", | ||
confirmInvitation: jest.fn(), | ||
invitationButtonTestId: "invitation-button", | ||
}; | ||
|
||
const invitationButtonPropsDecline: InvitationButtonProps = { | ||
text: "decline", | ||
confirmInvitation: jest.fn(), | ||
invitationButtonTestId: "invitation-button", | ||
}; | ||
|
||
function RenderInvitationButton(props: InvitationButtonProps) { | ||
renderWithRedux(<InvitationButton text={props.text} confirmInvitation={props.confirmInvitation} invitationButtonTestId={props.invitationButtonTestId} />); | ||
} | ||
|
||
describe("InvitationButton", () => { | ||
it("should render the accept button", () => { | ||
RenderInvitationButton(invitationButtonPropsAccept); | ||
expect(screen.getByTestId(invitationButtonPropsAccept.invitationButtonTestId!)).toBeInTheDocument(); | ||
expect(screen.getByText("accept")).toBeInTheDocument(); | ||
}); | ||
|
||
it("should render the decline button", () => { | ||
RenderInvitationButton(invitationButtonPropsDecline); | ||
expect(screen.getByTestId(invitationButtonPropsDecline.invitationButtonTestId!)).toBeInTheDocument(); | ||
expect(screen.getByText("decline")).toBeInTheDocument(); | ||
}); | ||
|
||
it('should call confirmInvitation with "accept" when accept button is clicked', () => { | ||
RenderInvitationButton(invitationButtonPropsAccept); | ||
const button = screen.getByTestId(invitationButtonPropsAccept.invitationButtonTestId!); | ||
fireEvent.click(button); | ||
expect(invitationButtonPropsAccept.confirmInvitation).toHaveBeenCalledWith("accept"); | ||
}); | ||
|
||
it('should call confirmInvitation with "decline" when decline button is clicked', () => { | ||
RenderInvitationButton(invitationButtonPropsDecline); | ||
const button = screen.getByTestId(invitationButtonPropsDecline.invitationButtonTestId!); | ||
fireEvent.click(button); | ||
expect(invitationButtonPropsDecline.confirmInvitation).toHaveBeenCalledWith("decline"); | ||
}); | ||
}); |
78 changes: 78 additions & 0 deletions
78
__tests__/components/cards/challenge/_partials/ReplyToInvitation.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { render, screen, fireEvent } from "@testing-library/react"; | ||
import "@testing-library/jest-dom"; | ||
import { useDispatch } from "@/hooks/useTypedDispatch"; | ||
import { useMultiSelector } from "@/hooks/useTypedSelector"; | ||
import ReplyToInvitation, { InvitationProps } from "@/components/cards/challenge/_partials/ReplyToInvitation"; | ||
import { acceptInvitation, declineInvitation } from "@/store/feature/communities/challenges/invites.slice"; | ||
jest.mock("@/hooks/useTypedDispatch"); | ||
jest.mock("@/hooks/useTypedSelector"); | ||
jest.mock("@/store/services/teams.service"); | ||
jest.mock("@/store/feature/communities/challenges/invites.slice"); | ||
const mockDispatch = jest.fn(); | ||
|
||
(useDispatch as jest.Mock).mockReturnValue(mockDispatch); | ||
(useMultiSelector as jest.Mock).mockReturnValue({ | ||
challenge: { id: "challenge-id" }, | ||
team: { invites: [{ id: "invite-id", status: "PENDING" }] }, | ||
}); | ||
|
||
const invitationProps: InvitationProps = { | ||
invite_id: "invite-id", | ||
team_ref: "team/1", | ||
}; | ||
|
||
describe("ReplyToInvitation", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should render the ReplyToInvitation component", () => { | ||
render(<ReplyToInvitation {...invitationProps} />); | ||
expect(screen.getByTestId("reply-to-invitation")).toBeInTheDocument(); | ||
}); | ||
|
||
it("should render accept button and decline button in component", () => { | ||
render(<ReplyToInvitation {...invitationProps} />); | ||
const acceptButton = screen.getByTestId("reply-to-invitation"); | ||
const declineButton = screen.getByTestId("reply-to-invitation"); | ||
expect(acceptButton).toBeInTheDocument(); | ||
expect(declineButton).toBeInTheDocument(); | ||
}); | ||
|
||
it("should display loader when loading", async () => { | ||
render(<ReplyToInvitation {...invitationProps} />); | ||
expect(screen.getByTestId("loader")).toBeInTheDocument(); | ||
expect(await screen.findByTestId("loader")).not.toBeInTheDocument(); | ||
}); | ||
|
||
it("should call acceptInvitation when accept button is clicked", async () => { | ||
render(<ReplyToInvitation {...invitationProps} />); | ||
expect(await screen.findByTestId("loader")).not.toBeInTheDocument(); | ||
|
||
const acceptButton = screen.getByText("accept"); | ||
fireEvent.click(acceptButton); | ||
expect(await mockDispatch).toHaveBeenCalledWith(acceptInvitation("invite-id")); | ||
}); | ||
|
||
it("should call declineInvitation when decline button is clicked", async () => { | ||
render(<ReplyToInvitation {...invitationProps} />); | ||
expect(await screen.findByTestId("loader")).not.toBeInTheDocument(); | ||
|
||
const declineButton = screen.getByText("decline"); | ||
fireEvent.click(declineButton); | ||
expect(await mockDispatch).toHaveBeenCalledWith(declineInvitation("invite-id")); | ||
}); | ||
|
||
it("should not allow replies if the team is locked", async () => { | ||
(useMultiSelector as jest.Mock).mockReturnValueOnce({ | ||
challenge: { id: "challenge-id" }, | ||
team: { invites: [{ id: "invite-id", status: "PENDING" }], locked: true }, | ||
}); | ||
|
||
render(<ReplyToInvitation {...invitationProps} />); | ||
|
||
expect(await screen.findByTestId("loader")).not.toBeInTheDocument(); | ||
const invitationButtons = screen.queryAllByText("accept|decline"); | ||
expect(invitationButtons).toHaveLength(0); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.