1+ import { describe , it , beforeEach , expect , vi } from 'vitest' ;
2+ import { Provider } from 'react-redux' ;
3+ import UserTeamsTable from '../UserTeamsTable' ;
4+ import thunk from 'redux-thunk' ;
5+ import configureMockStore from 'redux-mock-store' ;
6+ import { render , screen , within , waitFor } from '@testing-library/react' ;
7+ import { userProfileMock } from '../../../../__tests__/mockStates.js' ;
8+ import axios from 'axios' ;
9+
10+ vi . mock ( 'react-redux' , async ( ) => {
11+ const actual = await vi . importActual ( 'react-redux' ) ;
12+ return {
13+ ...actual ,
14+ connect : ( ) => Component => Component ,
15+ } ;
16+ } ) ;
17+
18+ vi . mock ( 'axios' ) ;
19+ vi . mock ( 'react-toastify' , ( ) => ( {
20+ toast : {
21+ success : vi . fn ( ) ,
22+ error : vi . fn ( ) ,
23+ info : vi . fn ( ) ,
24+ } ,
25+ } ) ) ;
26+
27+ vi . mock ( 'utils/permissions' , ( ) => vi . fn ( ( ) => true ) ) ;
28+
29+ const mockStore = configureMockStore ( [ thunk ] ) ;
30+
31+ const mockUserProfile = {
32+ userProfile : {
33+ ...userProfileMock ,
34+ teams : [
35+ { _id : '1' , teamName : 'Team1' } ,
36+ { _id : '2' , teamName : 'Team2' } ,
37+ ] ,
38+ } ,
39+ setCodeValid : vi . fn ( ) ,
40+ onAssignTeamCode : vi . fn ( ) ,
41+ onUserVisibilitySwitch : vi . fn ( ) ,
42+ onButtonClick : vi . fn ( ) ,
43+ onDeleteClick : vi . fn ( ) ,
44+ fetchTeamCodeAllUsers : vi . fn ( ) ,
45+ canEditTeamCode : false ,
46+ canEditVisibility : false ,
47+ codeValid : true ,
48+ disabled : false ,
49+ edit : true ,
50+ role : 'Administrator' ,
51+ isVisible : false ,
52+ inputAutoComplete : [ ] ,
53+ inputAutoStatus : false ,
54+ isLoading : false ,
55+ } ;
56+
57+ beforeEach ( ( ) => {
58+ vi . clearAllMocks ( ) ;
59+ axios . get . mockResolvedValue ( { data : { } } ) ;
60+ axios . patch . mockResolvedValue ( { data : { } } ) ;
61+ } ) ;
62+
63+ const renderComponent = mockProps => {
64+ const store = mockStore ( {
65+ auth : {
66+ user : {
67+ role : 'Owner' ,
68+ permissions : {
69+ frontPermissions : [ ] ,
70+ } ,
71+ } ,
72+ } ,
73+ } ) ;
74+
75+ return render (
76+ < Provider store = { store } >
77+ < UserTeamsTable
78+ canEditVisibility = { mockProps . canEditVisibility }
79+ isVisible = { mockProps . isVisible }
80+ renderedOn = { Date . now ( ) }
81+ edit = { mockProps . edit }
82+ role = { mockProps . role }
83+ disabled = { mockProps . disabled }
84+ canEditTeamCode = { mockProps . canEditTeamCode }
85+ userProfile = {
86+ mockProps . userProfile
87+ ? {
88+ ...mockProps . userProfile ,
89+ teamCode : '' ,
90+ }
91+ : null
92+ }
93+ codeValid = { mockProps . codeValid }
94+ hasPermission = { vi . fn ( ( ) => true ) }
95+ setCodeValid = { mockProps . setCodeValid }
96+ onAssignTeamCode = { mockProps . onAssignTeamCode }
97+ onUserVisibilitySwitch = { mockProps . onUserVisibilitySwitch }
98+ onButtonClick = { mockProps . onButtonClick }
99+ onDeleteClick = { mockProps . onDeleteClick }
100+ fetchTeamCodeAllUsers = { mockProps . fetchTeamCodeAllUsers }
101+ inputAutoComplete = { mockProps . inputAutoComplete }
102+ inputAutoStatus = { mockProps . inputAutoStatus }
103+ isLoading = { mockProps . isLoading }
104+ />
105+ </ Provider >
106+ ) ;
107+ } ;
108+
109+ describe ( 'User Teams Table Component' , ( ) => {
110+ it ( 'render without crashing' , async ( ) => {
111+ const { unmount } = renderComponent ( mockUserProfile ) ;
112+ await waitFor ( ( ) => {
113+ expect ( screen . getByRole ( 'table' ) ) . toBeInTheDocument ( ) ;
114+ } ) ;
115+ unmount ( ) ;
116+ } ) ;
117+
118+ it ( 'renders correct number of teams the user is assigned to' , async ( ) => {
119+ renderComponent ( mockUserProfile ) ;
120+
121+ await waitFor ( ( ) => {
122+ const rows = within ( screen . getByRole ( 'table' ) ) . getAllByRole ( 'row' ) ;
123+ expect ( rows . length ) . toBe ( 3 ) ;
124+ } ) ;
125+ } ) ;
126+
127+ it ( 'renders correct team names' , async ( ) => {
128+ renderComponent ( mockUserProfile ) ;
129+
130+ await waitFor ( ( ) => {
131+ const teamRows = within ( screen . getByRole ( 'table' ) ) . getAllByRole ( 'row' ) ;
132+ expect ( teamRows . length ) . toBe ( 3 ) ;
133+ const teamNames = [
134+ teamRows [ 1 ] . cells [ 1 ] . textContent ,
135+ teamRows [ 2 ] . cells [ 1 ] . textContent ,
136+ ] ;
137+
138+ expect ( teamNames ) . toEqual ( [ 'Team1' , 'Team2' ] ) ;
139+ } ) ;
140+ } ) ;
141+
142+ it ( 'visibility toggle appears with permission' , async ( ) => {
143+ const modifiedMock = {
144+ ...mockUserProfile ,
145+ canEditVisibility : true ,
146+ } ;
147+
148+ renderComponent ( modifiedMock ) ;
149+ await waitFor ( ( ) => {
150+ const visibilityLabel = screen . getByText ( 'Visibility' ) ;
151+ expect ( visibilityLabel ) . toBeInTheDocument ( ) ;
152+ } ) ;
153+ } ) ;
154+
155+ it ( 'renders team code input field with correct placeholder' , async ( ) => {
156+ renderComponent ( mockUserProfile ) ;
157+
158+ await waitFor ( ( ) => {
159+ const teamCodeInput = screen . getByPlaceholderText ( 'X-XXX' ) ;
160+ expect ( teamCodeInput ) . toBeInTheDocument ( ) ;
161+ expect ( teamCodeInput . tagName ) . toBe ( 'INPUT' ) ;
162+ expect ( teamCodeInput . type ) . toBe ( 'text' ) ;
163+ expect ( teamCodeInput . id ) . toBe ( 'teamCode' ) ;
164+ } ) ;
165+ } ) ;
166+
167+ it ( 'renders table headers with correct column titles' , async ( ) => {
168+ renderComponent ( mockUserProfile ) ;
169+
170+ await waitFor ( ( ) => {
171+ const headerCells = within ( screen . getByRole ( 'table' ) ) . getAllByRole ( 'columnheader' ) ;
172+ expect ( headerCells . length ) . toBe ( 4 ) ;
173+ expect ( headerCells [ 0 ] . textContent ) . toBe ( '#' ) ;
174+ expect ( headerCells [ 1 ] . textContent ) . toBe ( 'Team Name' ) ;
175+ expect ( headerCells [ 2 ] . textContent ) . toBe ( 'Members' ) ;
176+ expect ( headerCells [ 3 ] . textContent ) . toBe ( '' ) ;
177+ } ) ;
178+ } ) ;
179+ } ) ;
0 commit comments