Skip to content

Commit

Permalink
changes to getTeamsByUserRole and getRatingsByUserCohort
Browse files Browse the repository at this point in the history
  • Loading branch information
shema-surge committed Oct 28, 2024
1 parent 5deff7a commit e21ec0b
Show file tree
Hide file tree
Showing 16 changed files with 3,839 additions and 950 deletions.
9 changes: 6 additions & 3 deletions src/Mutations/Ratings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,13 @@ export const REJECT_RATING = gql`
}
`;

export const FETCH_SPRINTS = gql`
query fetchSprints($orgToken: String!){
fetchSprints(orgToken: $orgToken)
export const GET_RATINGS_BY_USER_COHORT = gql`
query getRatingsByUserCohort($orgToken: String!){
getRatingsByUserCohort(orgToken: $orgToken){
id
sprint
}
}
`

export const ADD_RATINGS_BY_FILE = gql`
Expand Down
6 changes: 3 additions & 3 deletions src/Mutations/teamMutation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ export const DeleteTeam = gql`
}
`;

export const GET_TEAMS_BY_ROLE = gql`
query getTeamsByRole($orgToken: String!) {
getTeamsByRole(orgToken: $orgToken){
export const GET_TEAMS_BY_USER_ROLE = gql`
query getTeamsByUserRole($orgToken: String!) {
getTeamsByUserRole(orgToken: $orgToken){
id
name
members {
Expand Down
61 changes: 34 additions & 27 deletions src/components/BulkRatingModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { useLazyQuery, useMutation } from "@apollo/client"
import React, { useEffect, useState } from "react"
import { useTranslation } from "react-i18next"
import * as XLSX from "xlsx"
import { ADD_RATINGS_BY_FILE, FETCH_SPRINTS } from "../Mutations/Ratings"
import { ADD_RATINGS_BY_FILE, GET_RATINGS_BY_USER_COHORT } from "../Mutations/Ratings"
import { toast } from "react-toastify"
import { GET_TEAMS_BY_ROLE } from "../Mutations/teamMutation"
import { GET_TEAMS_BY_USER_ROLE } from "../Mutations/teamMutation"

type BulkRatingModalProps = {
bulkRateModal: boolean,
Expand All @@ -20,19 +20,19 @@ const orgToken = localStorage.getItem('orgToken')

const BulkRatingModal = ({ bulkRateModal, setBulkRateModal }: BulkRatingModalProps) => {
const { t } = useTranslation()
const [fetchSprints, { data: sprints, loading: loadingSprints, error: sprintsError }] = useLazyQuery(FETCH_SPRINTS, {
const [getRatingsByUserCohort, { data: ratings, loading: loadingRatings, error: ratingsError }] = useLazyQuery(GET_RATINGS_BY_USER_COHORT, {
variables: {
orgToken
},
fetchPolicy: 'network-only',
})
const [getTeamsByRole, {data: teams, loading: loadingTeams, error: teamsError}] = useLazyQuery(GET_TEAMS_BY_ROLE, {
const [getTeamsByUserRole, {data: teams, loading: loadingTeams, error: teamsError}] = useLazyQuery(GET_TEAMS_BY_USER_ROLE, {
variables: {
orgToken
},
fetchPolicy: 'network-only',
})
const [addRatingsByFile, { data: ratings, loading: loadingRatings, error: ratingsError }] = useMutation(ADD_RATINGS_BY_FILE)
const [addRatingsByFile, { data: bulkRatings, loading: loadingBulkRatings, error: bulkRatingsError }] = useMutation(ADD_RATINGS_BY_FILE)
const [formData, setFormData] = useState<AddRatingsByFileFormData>({
sprint: '',
file: null
Expand All @@ -51,7 +51,7 @@ const BulkRatingModal = ({ bulkRateModal, setBulkRateModal }: BulkRatingModalPro
orgToken
},
})
fetchSprints()
getRatingsByUserCohort()
toast.success("Rating completed succefully")
} catch (err: any) {
toast.error(err?.message)
Expand All @@ -61,7 +61,7 @@ const BulkRatingModal = ({ bulkRateModal, setBulkRateModal }: BulkRatingModalPro
const downloadTeamFile = async(e: any)=>{
try{
if(selectedTeam === '') throw new Error("No Team was selected")
const team = teams.getTeamsByRole.find((team:any)=>team.id === selectedTeam)
const team = teams.getTeamsByUserRole.find((team:any)=>team.id === selectedTeam)
const rows: any = []
team.members.forEach((member: any)=>{
console.log(member)
Expand Down Expand Up @@ -91,8 +91,8 @@ const BulkRatingModal = ({ bulkRateModal, setBulkRateModal }: BulkRatingModalPro
}

useEffect(() => {
fetchSprints()
getTeamsByRole()
getRatingsByUserCohort()
getTeamsByUserRole()
}, [])

return (
Expand All @@ -105,10 +105,10 @@ const BulkRatingModal = ({ bulkRateModal, setBulkRateModal }: BulkRatingModalPro
<hr className="w-full my-3 border-b bg-primary" />
</div>
<div>
<form className="flex flex-col gap-5" onSubmit={saveRatings}>
<form data-testid="bulRating-form" className="flex flex-col gap-5" onSubmit={saveRatings}>
<div className="flex flex-col gap-1">
<label>Choose a sprint</label>
<select className="p-2 text-black dark:text-white rounded-lg bg-white dark:bg-dark border-2 border-primary"
<select data-testid="select-sprint" className="p-2 text-black dark:text-white rounded-lg bg-white dark:bg-dark border-2 border-primary"
defaultValue={""}
onChange={(e) => {
e.preventDefault()
Expand All @@ -117,55 +117,62 @@ const BulkRatingModal = ({ bulkRateModal, setBulkRateModal }: BulkRatingModalPro
>
<option>Choose a sprint</option>
{
sprints && !sprints.fetchSprints.length ?
<option value={1}>Sprint 1</option>
ratings && !ratings.getRatingsByUserCohort.length ?
<option data-testid="sprint-default-option" value={1}>Sprint 1</option>
: ''
}
{
sprints && sprints.fetchSprints.length ?
[...sprints.fetchSprints, sprints.fetchSprints[sprints.fetchSprints.length - 1] + 1].map((sprint: number) =>
<option key={sprint} value={sprint}>Sprint {sprint}</option>
ratings && ratings.getRatingsByUserCohort.length ?
[...ratings.getRatingsByUserCohort].map((rating: any) =>
<option data-testid={`sprint-option-${rating.id}`} key={rating.id} value={rating.sprint}>Sprint {rating.sprint}</option>
)
: ''
}
{
loadingSprints ?
<option>Loading ...</option>
ratings && ratings.getRatingsByUserCohort.length ?
<option data-testid="sprint-new-option" value={[...ratings.getRatingsByUserCohort].pop().sprint+1}>Sprint {[...ratings.getRatingsByUserCohort].pop().sprint+1}</option>
: ''
}
{
sprintsError ?
<option>No sprints found...</option>
loadingRatings ?
<option data-testid="sprint-loading-option">Loading ...</option>
: ''
}
{
ratingsError ?
<option data-testid="sprint-error-option">No sprints found...</option>
: ''
}
</select>
</div>
<div className="flex items-center justify-between">
<input
data-testid="file-input"
className="w-1/2 h-full bg-gray-600 rounded-md"
type="file"
onChange={(e) => {
const file = e.target.files?.[0]
setFormData({ ...formData, file: file ? file : null })
}}
accept=".xlsx, .xls"
>
</input>
<div className="flex gap-2">
<select className="p-2 text-sm text-black dark:text-white rounded-lg bg-white dark:bg-dark border-2 border-primary" defaultValue={""} onChange={(e)=>setSelectedTeam(e.target.value)}>
<option>Choose a team</option>
<select data-testid="select-team" className="p-2 text-sm text-black dark:text-white rounded-lg bg-white dark:bg-dark border-2 border-primary" defaultValue={""} onChange={(e)=>setSelectedTeam(e.target.value)}>
<option data-testid="team-default-option">Choose a team</option>
{
teams && teams.getTeamsByRole.length > 0 ?
teams.getTeamsByRole.map((team: any)=><option key={team.id} value={team.id}>{team.name}</option>)
teams && teams.getTeamsByUserRole.length > 0 ?
teams.getTeamsByUserRole.map((team: any)=><option data-testid={`team-option-${team.id}`} key={team.id} value={team.id}>{team.name}</option>)
: ''
}
</select>
<button type="button" onClick={downloadTeamFile} className="p-3 text-white rounded-lg bg-green-500 text-sm font-serif font-semibold">Download</button>
<button data-testid="download-button" type="button" onClick={downloadTeamFile} className="p-3 text-white rounded-lg bg-green-500 text-sm font-serif font-semibold">Download</button>
</div>
</div>

<div>
{
ratings && ratings.addRatingsByFile.RejectedRatings.length > 0 ?
bulkRatings && bulkRatings.addRatingsByFile.RejectedRatings.length > 0 ?
<div className="my-1 overflow-x-auto">
<table className="table-fixed min-w-full">
<caption className="caption-top text-left my-2">
Expand All @@ -181,7 +188,7 @@ const BulkRatingModal = ({ bulkRateModal, setBulkRateModal }: BulkRatingModalPro
</tr>
</thead>
<tbody>
{ratings.addRatingsByFile?.RejectedRatings.map((rating: any, index: number) =>
{bulkRatings.addRatingsByFile?.RejectedRatings.map((rating: any, index: number) =>
<tr key={index} className="text-red-400">
<td className="text-left py-1 px-2">{rating.email ? rating.email : "null"}</td>
<td className="text-left py-1 px-2">{rating.quantity ? rating.quantity : "null"}</td>
Expand Down
140 changes: 140 additions & 0 deletions tests/components/BulkRatingModal.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import React from "react"
import "@testing-library/jest-dom"
import { MockedProvider, MockedResponse } from "@apollo/client/testing"
import { render, fireEvent, screen, cleanup, waitFor } from "@testing-library/react"
import BulkRatingModal from "../../src/components/BulkRatingModal"
import { GET_RATINGS_BY_USER_COHORT } from "../../src/Mutations/Ratings"
import { GET_TEAMS_BY_USER_ROLE } from "../../src/Mutations/teamMutation"

const getRatingsByUserCohort: MockedResponse = {
request:{
query: GET_RATINGS_BY_USER_COHORT,
// variables: {
// orgToken: 'mocked_org_token'
// }
},
variableMatcher: () => true,
result:{
data: {
getRatingsByUserCohort: [
{
id: "1",
sprint: 1,
},
{
id: "2",
sprint: 2,
}
]
}
}
}

const getTeamsByUserRole = {
request: {
query: GET_TEAMS_BY_USER_ROLE,
// variables: {
// orgToken: "mocked_org_token"
// }
},
variableMatcher: () => true,
result:{
data: {
getTeamsByUserRole: [
{
id: "1",
name: "Team I",
members:[
{
email: "[email protected]",
role: "trainee"
},
{
email: "[email protected]",
role: "ttl"
},
{
email: "[email protected]",
role: "trainee"
},
]
},
{
id: "2",
name: "Team II",
members:[
{
email: "[email protected]",
role: "trainee"
},
{
email: "[email protected]",
role: "ttl"
},
{
email: "[email protected]",
role: "trainee"
},
]
}
]
}
}
}

jest.mock('react-toastify', () => ({
toast: {
success: jest.fn(),
error: jest.fn(),
}
}))

beforeEach(() => {
localStorage.setItem('auth_token', 'mocked_auth_token')
localStorage.setItem('orgToken', 'mocked_org_token')
localStorage.setItem('auth', JSON.stringify({
auth: true,
email: "[email protected]",
firstName: "Jack",
role: "admin",
userId: "1"
}))
})

afterEach(() => {
localStorage.clear()
cleanup()
})

describe("BulkRatingModal", () => {
const setBulkRateModel = jest.fn()

it("displays all sprints", async() => {
render(
<MockedProvider mocks={[getRatingsByUserCohort, getTeamsByUserRole]} addTypename={false}>
<BulkRatingModal
bulkRateModal={true}
setBulkRateModal={setBulkRateModel}
></BulkRatingModal>
</MockedProvider>
)
await waitFor(()=>{
expect(screen.getByTestId("sprint-option-1")).toBeInTheDocument()
expect(screen.getByTestId("sprint-option-2")).toBeInTheDocument()
})
})
it("displays teams", async() => {
render(
<MockedProvider mocks={[getRatingsByUserCohort, getTeamsByUserRole]} addTypename={false}>
<BulkRatingModal
bulkRateModal={true}
setBulkRateModal={setBulkRateModel}
></BulkRatingModal>
</MockedProvider>
)
await waitFor(()=>{
expect(screen.getByTestId("team-option-1")).toBeInTheDocument()
expect(screen.getByTestId("team-option-2")).toBeInTheDocument()
})
})
})
Loading

0 comments on commit e21ec0b

Please sign in to comment.