Skip to content

Commit e21ec0b

Browse files
committed
changes to getTeamsByUserRole and getRatingsByUserCohort
1 parent 5deff7a commit e21ec0b

16 files changed

+3839
-950
lines changed

src/Mutations/Ratings.tsx

+6-3
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,13 @@ export const REJECT_RATING = gql`
180180
}
181181
`;
182182

183-
export const FETCH_SPRINTS = gql`
184-
query fetchSprints($orgToken: String!){
185-
fetchSprints(orgToken: $orgToken)
183+
export const GET_RATINGS_BY_USER_COHORT = gql`
184+
query getRatingsByUserCohort($orgToken: String!){
185+
getRatingsByUserCohort(orgToken: $orgToken){
186+
id
187+
sprint
186188
}
189+
}
187190
`
188191

189192
export const ADD_RATINGS_BY_FILE = gql`

src/Mutations/teamMutation.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ export const DeleteTeam = gql`
5353
}
5454
`;
5555

56-
export const GET_TEAMS_BY_ROLE = gql`
57-
query getTeamsByRole($orgToken: String!) {
58-
getTeamsByRole(orgToken: $orgToken){
56+
export const GET_TEAMS_BY_USER_ROLE = gql`
57+
query getTeamsByUserRole($orgToken: String!) {
58+
getTeamsByUserRole(orgToken: $orgToken){
5959
id
6060
name
6161
members {

src/components/BulkRatingModal.tsx

+34-27
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { useLazyQuery, useMutation } from "@apollo/client"
22
import React, { useEffect, useState } from "react"
33
import { useTranslation } from "react-i18next"
44
import * as XLSX from "xlsx"
5-
import { ADD_RATINGS_BY_FILE, FETCH_SPRINTS } from "../Mutations/Ratings"
5+
import { ADD_RATINGS_BY_FILE, GET_RATINGS_BY_USER_COHORT } from "../Mutations/Ratings"
66
import { toast } from "react-toastify"
7-
import { GET_TEAMS_BY_ROLE } from "../Mutations/teamMutation"
7+
import { GET_TEAMS_BY_USER_ROLE } from "../Mutations/teamMutation"
88

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

2121
const BulkRatingModal = ({ bulkRateModal, setBulkRateModal }: BulkRatingModalProps) => {
2222
const { t } = useTranslation()
23-
const [fetchSprints, { data: sprints, loading: loadingSprints, error: sprintsError }] = useLazyQuery(FETCH_SPRINTS, {
23+
const [getRatingsByUserCohort, { data: ratings, loading: loadingRatings, error: ratingsError }] = useLazyQuery(GET_RATINGS_BY_USER_COHORT, {
2424
variables: {
2525
orgToken
2626
},
2727
fetchPolicy: 'network-only',
2828
})
29-
const [getTeamsByRole, {data: teams, loading: loadingTeams, error: teamsError}] = useLazyQuery(GET_TEAMS_BY_ROLE, {
29+
const [getTeamsByUserRole, {data: teams, loading: loadingTeams, error: teamsError}] = useLazyQuery(GET_TEAMS_BY_USER_ROLE, {
3030
variables: {
3131
orgToken
3232
},
3333
fetchPolicy: 'network-only',
3434
})
35-
const [addRatingsByFile, { data: ratings, loading: loadingRatings, error: ratingsError }] = useMutation(ADD_RATINGS_BY_FILE)
35+
const [addRatingsByFile, { data: bulkRatings, loading: loadingBulkRatings, error: bulkRatingsError }] = useMutation(ADD_RATINGS_BY_FILE)
3636
const [formData, setFormData] = useState<AddRatingsByFileFormData>({
3737
sprint: '',
3838
file: null
@@ -51,7 +51,7 @@ const BulkRatingModal = ({ bulkRateModal, setBulkRateModal }: BulkRatingModalPro
5151
orgToken
5252
},
5353
})
54-
fetchSprints()
54+
getRatingsByUserCohort()
5555
toast.success("Rating completed succefully")
5656
} catch (err: any) {
5757
toast.error(err?.message)
@@ -61,7 +61,7 @@ const BulkRatingModal = ({ bulkRateModal, setBulkRateModal }: BulkRatingModalPro
6161
const downloadTeamFile = async(e: any)=>{
6262
try{
6363
if(selectedTeam === '') throw new Error("No Team was selected")
64-
const team = teams.getTeamsByRole.find((team:any)=>team.id === selectedTeam)
64+
const team = teams.getTeamsByUserRole.find((team:any)=>team.id === selectedTeam)
6565
const rows: any = []
6666
team.members.forEach((member: any)=>{
6767
console.log(member)
@@ -91,8 +91,8 @@ const BulkRatingModal = ({ bulkRateModal, setBulkRateModal }: BulkRatingModalPro
9191
}
9292

9393
useEffect(() => {
94-
fetchSprints()
95-
getTeamsByRole()
94+
getRatingsByUserCohort()
95+
getTeamsByUserRole()
9696
}, [])
9797

9898
return (
@@ -105,10 +105,10 @@ const BulkRatingModal = ({ bulkRateModal, setBulkRateModal }: BulkRatingModalPro
105105
<hr className="w-full my-3 border-b bg-primary" />
106106
</div>
107107
<div>
108-
<form className="flex flex-col gap-5" onSubmit={saveRatings}>
108+
<form data-testid="bulRating-form" className="flex flex-col gap-5" onSubmit={saveRatings}>
109109
<div className="flex flex-col gap-1">
110110
<label>Choose a sprint</label>
111-
<select className="p-2 text-black dark:text-white rounded-lg bg-white dark:bg-dark border-2 border-primary"
111+
<select data-testid="select-sprint" className="p-2 text-black dark:text-white rounded-lg bg-white dark:bg-dark border-2 border-primary"
112112
defaultValue={""}
113113
onChange={(e) => {
114114
e.preventDefault()
@@ -117,55 +117,62 @@ const BulkRatingModal = ({ bulkRateModal, setBulkRateModal }: BulkRatingModalPro
117117
>
118118
<option>Choose a sprint</option>
119119
{
120-
sprints && !sprints.fetchSprints.length ?
121-
<option value={1}>Sprint 1</option>
120+
ratings && !ratings.getRatingsByUserCohort.length ?
121+
<option data-testid="sprint-default-option" value={1}>Sprint 1</option>
122122
: ''
123123
}
124124
{
125-
sprints && sprints.fetchSprints.length ?
126-
[...sprints.fetchSprints, sprints.fetchSprints[sprints.fetchSprints.length - 1] + 1].map((sprint: number) =>
127-
<option key={sprint} value={sprint}>Sprint {sprint}</option>
125+
ratings && ratings.getRatingsByUserCohort.length ?
126+
[...ratings.getRatingsByUserCohort].map((rating: any) =>
127+
<option data-testid={`sprint-option-${rating.id}`} key={rating.id} value={rating.sprint}>Sprint {rating.sprint}</option>
128128
)
129129
: ''
130130
}
131131
{
132-
loadingSprints ?
133-
<option>Loading ...</option>
132+
ratings && ratings.getRatingsByUserCohort.length ?
133+
<option data-testid="sprint-new-option" value={[...ratings.getRatingsByUserCohort].pop().sprint+1}>Sprint {[...ratings.getRatingsByUserCohort].pop().sprint+1}</option>
134134
: ''
135135
}
136136
{
137-
sprintsError ?
138-
<option>No sprints found...</option>
137+
loadingRatings ?
138+
<option data-testid="sprint-loading-option">Loading ...</option>
139+
: ''
140+
}
141+
{
142+
ratingsError ?
143+
<option data-testid="sprint-error-option">No sprints found...</option>
139144
: ''
140145
}
141146
</select>
142147
</div>
143148
<div className="flex items-center justify-between">
144149
<input
150+
data-testid="file-input"
145151
className="w-1/2 h-full bg-gray-600 rounded-md"
146152
type="file"
147153
onChange={(e) => {
148154
const file = e.target.files?.[0]
149155
setFormData({ ...formData, file: file ? file : null })
150156
}}
157+
accept=".xlsx, .xls"
151158
>
152159
</input>
153160
<div className="flex gap-2">
154-
<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)}>
155-
<option>Choose a team</option>
161+
<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)}>
162+
<option data-testid="team-default-option">Choose a team</option>
156163
{
157-
teams && teams.getTeamsByRole.length > 0 ?
158-
teams.getTeamsByRole.map((team: any)=><option key={team.id} value={team.id}>{team.name}</option>)
164+
teams && teams.getTeamsByUserRole.length > 0 ?
165+
teams.getTeamsByUserRole.map((team: any)=><option data-testid={`team-option-${team.id}`} key={team.id} value={team.id}>{team.name}</option>)
159166
: ''
160167
}
161168
</select>
162-
<button type="button" onClick={downloadTeamFile} className="p-3 text-white rounded-lg bg-green-500 text-sm font-serif font-semibold">Download</button>
169+
<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>
163170
</div>
164171
</div>
165172

166173
<div>
167174
{
168-
ratings && ratings.addRatingsByFile.RejectedRatings.length > 0 ?
175+
bulkRatings && bulkRatings.addRatingsByFile.RejectedRatings.length > 0 ?
169176
<div className="my-1 overflow-x-auto">
170177
<table className="table-fixed min-w-full">
171178
<caption className="caption-top text-left my-2">
@@ -181,7 +188,7 @@ const BulkRatingModal = ({ bulkRateModal, setBulkRateModal }: BulkRatingModalPro
181188
</tr>
182189
</thead>
183190
<tbody>
184-
{ratings.addRatingsByFile?.RejectedRatings.map((rating: any, index: number) =>
191+
{bulkRatings.addRatingsByFile?.RejectedRatings.map((rating: any, index: number) =>
185192
<tr key={index} className="text-red-400">
186193
<td className="text-left py-1 px-2">{rating.email ? rating.email : "null"}</td>
187194
<td className="text-left py-1 px-2">{rating.quantity ? rating.quantity : "null"}</td>
+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import React from "react"
2+
import "@testing-library/jest-dom"
3+
import { MockedProvider, MockedResponse } from "@apollo/client/testing"
4+
import { render, fireEvent, screen, cleanup, waitFor } from "@testing-library/react"
5+
import BulkRatingModal from "../../src/components/BulkRatingModal"
6+
import { GET_RATINGS_BY_USER_COHORT } from "../../src/Mutations/Ratings"
7+
import { GET_TEAMS_BY_USER_ROLE } from "../../src/Mutations/teamMutation"
8+
9+
const getRatingsByUserCohort: MockedResponse = {
10+
request:{
11+
query: GET_RATINGS_BY_USER_COHORT,
12+
// variables: {
13+
// orgToken: 'mocked_org_token'
14+
// }
15+
},
16+
variableMatcher: () => true,
17+
result:{
18+
data: {
19+
getRatingsByUserCohort: [
20+
{
21+
id: "1",
22+
sprint: 1,
23+
},
24+
{
25+
id: "2",
26+
sprint: 2,
27+
}
28+
]
29+
}
30+
}
31+
}
32+
33+
const getTeamsByUserRole = {
34+
request: {
35+
query: GET_TEAMS_BY_USER_ROLE,
36+
// variables: {
37+
// orgToken: "mocked_org_token"
38+
// }
39+
},
40+
variableMatcher: () => true,
41+
result:{
42+
data: {
43+
getTeamsByUserRole: [
44+
{
45+
id: "1",
46+
name: "Team I",
47+
members:[
48+
{
49+
50+
role: "trainee"
51+
},
52+
{
53+
54+
role: "ttl"
55+
},
56+
{
57+
58+
role: "trainee"
59+
},
60+
]
61+
},
62+
{
63+
id: "2",
64+
name: "Team II",
65+
members:[
66+
{
67+
68+
role: "trainee"
69+
},
70+
{
71+
72+
role: "ttl"
73+
},
74+
{
75+
76+
role: "trainee"
77+
},
78+
]
79+
}
80+
]
81+
}
82+
}
83+
}
84+
85+
jest.mock('react-toastify', () => ({
86+
toast: {
87+
success: jest.fn(),
88+
error: jest.fn(),
89+
}
90+
}))
91+
92+
beforeEach(() => {
93+
localStorage.setItem('auth_token', 'mocked_auth_token')
94+
localStorage.setItem('orgToken', 'mocked_org_token')
95+
localStorage.setItem('auth', JSON.stringify({
96+
auth: true,
97+
98+
firstName: "Jack",
99+
role: "admin",
100+
userId: "1"
101+
}))
102+
})
103+
104+
afterEach(() => {
105+
localStorage.clear()
106+
cleanup()
107+
})
108+
109+
describe("BulkRatingModal", () => {
110+
const setBulkRateModel = jest.fn()
111+
112+
it("displays all sprints", async() => {
113+
render(
114+
<MockedProvider mocks={[getRatingsByUserCohort, getTeamsByUserRole]} addTypename={false}>
115+
<BulkRatingModal
116+
bulkRateModal={true}
117+
setBulkRateModal={setBulkRateModel}
118+
></BulkRatingModal>
119+
</MockedProvider>
120+
)
121+
await waitFor(()=>{
122+
expect(screen.getByTestId("sprint-option-1")).toBeInTheDocument()
123+
expect(screen.getByTestId("sprint-option-2")).toBeInTheDocument()
124+
})
125+
})
126+
it("displays teams", async() => {
127+
render(
128+
<MockedProvider mocks={[getRatingsByUserCohort, getTeamsByUserRole]} addTypename={false}>
129+
<BulkRatingModal
130+
bulkRateModal={true}
131+
setBulkRateModal={setBulkRateModel}
132+
></BulkRatingModal>
133+
</MockedProvider>
134+
)
135+
await waitFor(()=>{
136+
expect(screen.getByTestId("team-option-1")).toBeInTheDocument()
137+
expect(screen.getByTestId("team-option-2")).toBeInTheDocument()
138+
})
139+
})
140+
})

0 commit comments

Comments
 (0)