File tree Expand file tree Collapse file tree 3 files changed +35
-14
lines changed
Expand file tree Collapse file tree 3 files changed +35
-14
lines changed Original file line number Diff line number Diff line change @@ -34,6 +34,17 @@ import {getCertifications} from "../api/certification.js";
3434
3535const AppContext = React . createContext ( ) ;
3636
37+ function getSessionCookieValue ( name ) {
38+ const cookies = document ?. cookie ?. split ( ';' ) ;
39+ for ( let i = 0 ; i < cookies . length ; i ++ ) {
40+ const cookie = cookies [ i ] . trim ( ) ;
41+ if ( cookie . startsWith ( name + '=' ) ) {
42+ return decodeURIComponent ( cookie . substring ( name . length + 1 ) ) ;
43+ }
44+ }
45+ return null ;
46+ }
47+
3748const AppContextProvider = props => {
3849 const [ state , dispatch ] = useReducer (
3950 reducer ,
@@ -64,12 +75,17 @@ const AppContextProvider = props => {
6475 useEffect ( ( ) => {
6576 const getCsrf = async ( ) => {
6677 if ( ! csrf ) {
67- const res = await fetch ( url , {
68- responseType : 'text' ,
69- credentials : 'include'
70- } ) ;
71- if ( res && res . ok ) {
72- dispatch ( { type : SET_CSRF , payload : await res . text ( ) } ) ;
78+ const payload = getSessionCookieValue ( '_csrf' ) ;
79+ if ( payload ) {
80+ dispatch ( { type : SET_CSRF , payload } ) ;
81+ } else {
82+ const res = await fetch ( url , {
83+ responseType : 'text' ,
84+ credentials : 'include'
85+ } ) ;
86+ if ( res && res . ok ) {
87+ dispatch ( { type : SET_CSRF , payload : await res . text ( ) } ) ;
88+ }
7389 }
7490 }
7591 } ;
Original file line number Diff line number Diff line change @@ -14,26 +14,28 @@ const userStateWithPermission = {
1414
1515it ( 'renders correctly' , ( ) => {
1616 const mockDate = new Date ( 2022 , 1 , 1 ) ;
17- const spy = vi . spyOn ( global , 'Date' ) . mockImplementation ( ( ) => mockDate ) ;
17+ vi . useFakeTimers ( ) ;
18+ vi . setSystemTime ( mockDate ) ;
1819
1920 snapshot (
2021 < AppContextProvider value = { userStateWithPermission } >
2122 < AnniversaryReportPage />
2223 </ AppContextProvider >
2324 ) ;
2425
25- spy . mockRestore ( ) ;
26+ vi . useRealTimers ( ) ;
2627} ) ;
2728
2829it ( 'renders an error if user does not have appropriate permission' , ( ) => {
2930 const mockDate = new Date ( 2022 , 1 , 1 ) ;
30- const spy = vi . spyOn ( global , 'Date' ) . mockImplementation ( ( ) => mockDate ) ;
31+ vi . useFakeTimers ( ) ;
32+ vi . setSystemTime ( mockDate ) ;
3133
3234 snapshot (
3335 < AppContextProvider >
3436 < AnniversaryReportPage />
3537 </ AppContextProvider >
3638 ) ;
3739
38- spy . mockRestore ( ) ;
40+ vi . useRealTimers ( ) ;
3941} ) ;
Original file line number Diff line number Diff line change 11import React from 'react' ;
2+ import { vi } from 'vitest' ;
23import BirthdayReportPage from './BirthdayReportPage' ;
34import { AppContextProvider } from '../context/AppContext' ;
45
@@ -14,26 +15,28 @@ const userStateWithPermission = {
1415
1516it ( 'renders correctly' , ( ) => {
1617 const mockDate = new Date ( 2022 , 1 , 1 ) ;
17- const spy = vi . spyOn ( global , 'Date' ) . mockImplementation ( ( ) => mockDate ) ;
18+ vi . useFakeTimers ( ) ;
19+ vi . setSystemTime ( mockDate ) ;
1820
1921 snapshot (
2022 < AppContextProvider value = { userStateWithPermission } >
2123 < BirthdayReportPage />
2224 </ AppContextProvider >
2325 ) ;
2426
25- spy . mockRestore ( ) ;
27+ vi . useRealTimers ( ) ;
2628} ) ;
2729
2830it ( 'renders an error if user does not have appropriate permission' , ( ) => {
2931 const mockDate = new Date ( 2022 , 1 , 1 ) ;
30- const spy = vi . spyOn ( global , 'Date' ) . mockImplementation ( ( ) => mockDate ) ;
32+ vi . useFakeTimers ( ) ;
33+ vi . setSystemTime ( mockDate ) ;
3134
3235 snapshot (
3336 < AppContextProvider >
3437 < BirthdayReportPage />
3538 </ AppContextProvider >
3639 ) ;
3740
38- spy . mockRestore ( ) ;
41+ vi . useRealTimers ( ) ;
3942} ) ;
You can’t perform that action at this time.
0 commit comments