11import { getCsrfToken } from 'utils/utility'
22
3+ jest . mock ( 'api/getCsrfToken' , ( ) => ( {
4+ getInitialCsrfToken : jest . fn ( ( ) => Promise . resolve ( 'abc123' ) ) ,
5+ } ) )
6+
37describe ( 'utility tests' , ( ) => {
48 beforeEach ( ( ) => {
59 jest . clearAllMocks ( )
@@ -9,33 +13,39 @@ describe('utility tests', () => {
913 } )
1014 } )
1115
12- test ( 'returns CSRF token when it exists in cookies' , ( ) => {
16+ test ( 'returns CSRF token when it exists in cookies' , async ( ) => {
1317 document . cookie = 'csrftoken=abc123; otherkey=xyz789'
14- expect ( getCsrfToken ( ) ) . toBe ( 'abc123' )
18+ const result = await getCsrfToken ( )
19+ expect ( result ) . toBe ( 'abc123' )
1520 } )
1621
17- test ( 'returns undefined when no cookies are present' , ( ) => {
22+ test ( 'returns new token when no cookies are present' , async ( ) => {
1823 document . cookie = ''
19- expect ( getCsrfToken ( ) ) . toBeUndefined ( )
24+ const result = await getCsrfToken ( )
25+ expect ( result ) . toBe ( 'abc123' )
2026 } )
2127
22- test ( 'returns undefined when csrftoken cookie is not present' , ( ) => {
28+ test ( 'returns new csrftoken when csrftoken cookie is not present' , async ( ) => {
2329 document . cookie = 'someid=xyz789; othercookie=123'
24- expect ( getCsrfToken ( ) ) . toBeUndefined ( )
30+ const result = await getCsrfToken ( )
31+ expect ( result ) . toBe ( 'abc123' )
2532 } )
2633
27- test ( 'returns first csrftoken value when multiple cookies exist' , ( ) => {
34+ test ( 'returns first csrftoken value when multiple cookies exist' , async ( ) => {
2835 document . cookie = 'csrftoken=first; csrftoken=second; otherid=xyz789'
29- expect ( getCsrfToken ( ) ) . toBe ( 'first' )
36+ const result = await getCsrfToken ( )
37+ expect ( result ) . toBe ( 'first' )
3038 } )
3139
32- test ( 'handles cookie with no value' , ( ) => {
40+ test ( 'handles cookie with no value' , async ( ) => {
3341 document . cookie = 'csrftoken=; otherid=xyz789'
34- expect ( getCsrfToken ( ) ) . toBe ( '' )
42+ const result = await getCsrfToken ( )
43+ expect ( result ) . toBe ( 'abc123' )
3544 } )
3645
37- test ( 'handles malformed cookie string' , ( ) => {
46+ test ( 'handles malformed cookie string' , async ( ) => {
3847 document . cookie = 'csrftoken; otherid=xyz789'
39- expect ( getCsrfToken ( ) ) . toBeUndefined ( )
48+ const result = await getCsrfToken ( )
49+ expect ( result ) . toBe ( 'abc123' )
4050 } )
4151} )
0 commit comments