1
1
import React from 'react' ;
2
2
import LoginPage from './LoginPage' ;
3
- import { render } from '@testing-library/react' ;
3
+ import { act , fireEvent , render , waitForElement } from '@testing-library/react' ;
4
4
import { useAppState } from '../../state' ;
5
5
import { useLocation , useHistory } from 'react-router-dom' ;
6
6
@@ -27,22 +27,22 @@ describe('the LoginPage component', () => {
27
27
28
28
describe ( 'with auth enabled' , ( ) => {
29
29
it ( 'should redirect to "/" when there is a user ' , ( ) => {
30
- process . env . REACT_APP_USE_FIREBASE_AUTH = 'true ' ;
30
+ process . env . REACT_APP_SET_AUTH = 'firebase ' ;
31
31
mockUseAppState . mockImplementation ( ( ) => ( { user : { } , signIn : ( ) => Promise . resolve ( ) , isAuthReady : true } ) ) ;
32
32
render ( < LoginPage /> ) ;
33
33
expect ( mockReplace ) . toHaveBeenCalledWith ( '/' ) ;
34
34
} ) ;
35
35
36
36
it ( 'should render the login page when there is no user' , ( ) => {
37
- process . env . REACT_APP_USE_FIREBASE_AUTH = 'true ' ;
37
+ process . env . REACT_APP_SET_AUTH = 'firebase ' ;
38
38
mockUseAppState . mockImplementation ( ( ) => ( { user : null , signIn : ( ) => Promise . resolve ( ) , isAuthReady : true } ) ) ;
39
39
const { getByText } = render ( < LoginPage /> ) ;
40
40
expect ( mockReplace ) . not . toHaveBeenCalled ( ) ;
41
41
expect ( getByText ( 'Sign in with Google' ) ) . toBeTruthy ( ) ;
42
42
} ) ;
43
43
44
44
it ( 'should redirect the user to "/" after signIn when there is no previous location' , done => {
45
- process . env . REACT_APP_USE_FIREBASE_AUTH = 'true ' ;
45
+ process . env . REACT_APP_SET_AUTH = 'firebase ' ;
46
46
mockUseAppState . mockImplementation ( ( ) => ( { user : null , signIn : ( ) => Promise . resolve ( ) , isAuthReady : true } ) ) ;
47
47
const { getByText } = render ( < LoginPage /> ) ;
48
48
getByText ( 'Sign in with Google' ) . click ( ) ;
@@ -53,7 +53,7 @@ describe('the LoginPage component', () => {
53
53
} ) ;
54
54
55
55
it ( 'should redirect the user to their previous location after signIn' , done => {
56
- process . env . REACT_APP_USE_FIREBASE_AUTH = 'true ' ;
56
+ process . env . REACT_APP_SET_AUTH = 'firebase ' ;
57
57
mockUseLocation . mockImplementation ( ( ) => ( { state : { from : { pathname : '/room/test' } } } ) ) ;
58
58
mockUseAppState . mockImplementation ( ( ) => ( { user : null , signIn : ( ) => Promise . resolve ( ) , isAuthReady : true } ) ) ;
59
59
const { getByText } = render ( < LoginPage /> ) ;
@@ -65,16 +65,55 @@ describe('the LoginPage component', () => {
65
65
} ) ;
66
66
67
67
it ( 'should not render anything when isAuthReady is false' , ( ) => {
68
- process . env . REACT_APP_USE_FIREBASE_AUTH = 'true ' ;
68
+ process . env . REACT_APP_SET_AUTH = 'firebase ' ;
69
69
mockUseAppState . mockImplementation ( ( ) => ( { user : null , signIn : ( ) => Promise . resolve ( ) , isAuthReady : false } ) ) ;
70
70
const { container } = render ( < LoginPage /> ) ;
71
71
expect ( mockReplace ) . not . toHaveBeenCalled ( ) ;
72
72
expect ( container . children [ 0 ] ) . toBe ( undefined ) ;
73
73
} ) ;
74
74
} ) ;
75
75
76
+ describe ( 'with passcode auth enabled' , ( ) => {
77
+ it ( 'should call sign in with the supplied passcode' , done => {
78
+ const mockSignin = jest . fn ( ( ) => Promise . resolve ( ) ) ;
79
+ process . env . REACT_APP_SET_AUTH = 'passcode' ;
80
+ mockUseAppState . mockImplementation ( ( ) => ( { user : null , signIn : mockSignin , isAuthReady : true } ) ) ;
81
+ const { getByLabelText, getByText } = render ( < LoginPage /> ) ;
82
+
83
+ act ( ( ) => {
84
+ fireEvent . change ( getByLabelText ( 'Passcode' ) , { target : { value : '1234' } } ) ;
85
+ } ) ;
86
+ act ( ( ) => {
87
+ fireEvent . submit ( getByText ( 'Submit' ) ) ;
88
+ } ) ;
89
+
90
+ setImmediate ( ( ) => {
91
+ expect ( mockSignin ) . toHaveBeenCalledWith ( '1234' ) ;
92
+ done ( ) ;
93
+ } ) ;
94
+ } ) ;
95
+
96
+ it ( 'should call render error messages when signin fails' , async ( ) => {
97
+ const mockSignin = jest . fn ( ( ) => Promise . reject ( new Error ( 'Test Error' ) ) ) ;
98
+ process . env . REACT_APP_SET_AUTH = 'passcode' ;
99
+ mockUseAppState . mockImplementation ( ( ) => ( { user : null , signIn : mockSignin , isAuthReady : true } ) ) ;
100
+ const { getByLabelText, getByText } = render ( < LoginPage /> ) ;
101
+
102
+ act ( ( ) => {
103
+ fireEvent . change ( getByLabelText ( 'Passcode' ) , { target : { value : '1234' } } ) ;
104
+ } ) ;
105
+
106
+ act ( ( ) => {
107
+ fireEvent . submit ( getByText ( 'Submit' ) ) ;
108
+ } ) ;
109
+
110
+ const element = await waitForElement ( ( ) => getByText ( 'Test Error' ) )
111
+ expect ( element ) . toBeTruthy ( )
112
+ } ) ;
113
+ } ) ;
114
+
76
115
it ( 'should redirect to "/" when auth is disabled' , ( ) => {
77
- process . env . REACT_APP_USE_FIREBASE_AUTH = 'false' ;
116
+ delete process . env . REACT_APP_SET_AUTH ;
78
117
mockUseAppState . mockImplementation ( ( ) => ( { user : null , signIn : ( ) => Promise . resolve ( ) , isAuthReady : true } ) ) ;
79
118
render ( < LoginPage /> ) ;
80
119
expect ( mockReplace ) . toHaveBeenCalledWith ( '/' ) ;
0 commit comments