2019-01-02 10:28:24 +03:00
import { mount } from 'enzyme' ;
2019-01-07 15:26:21 +03:00
import { main , getLanguage } from '../src/index' ;
2018-11-02 20:45:11 +03:00
2019-01-02 10:28:24 +03:00
const render = template => mount ( template ) ;
2019-01-24 21:05:36 +03:00
const data = { custom _logo : 'foo' , custom _login _info : '' } ;
2018-11-02 20:45:11 +03:00
2019-01-02 10:28:24 +03:00
describe ( 'index.jsx' , ( ) => {
2019-01-07 15:26:21 +03:00
test ( 'login loads when unauthenticated' , async ( done ) => {
2019-01-02 10:28:24 +03:00
const isAuthenticated = ( ) => false ;
const getRoot = jest . fn ( ( ) => Promise . resolve ( { data } ) ) ;
2018-11-02 20:45:11 +03:00
2019-01-02 10:28:24 +03:00
const api = { getRoot , isAuthenticated } ;
const wrapper = await main ( render , api ) ;
2018-11-02 20:45:11 +03:00
2019-01-02 10:28:24 +03:00
expect ( api . getRoot ) . toHaveBeenCalled ( ) ;
2019-01-07 15:26:21 +03:00
expect ( wrapper . find ( 'App' ) ) . toHaveLength ( 0 ) ;
2019-01-02 10:28:24 +03:00
expect ( wrapper . find ( 'Login' ) ) . toHaveLength ( 1 ) ;
2018-11-02 20:45:11 +03:00
2019-01-02 10:28:24 +03:00
const { src } = wrapper . find ( 'Login Brand img' ) . props ( ) ;
expect ( src ) . toContain ( data . custom _logo ) ;
done ( ) ;
} ) ;
2019-01-07 15:26:21 +03:00
test ( 'app loads when authenticated' , async ( done ) => {
2019-01-02 10:28:24 +03:00
const isAuthenticated = ( ) => true ;
const getRoot = jest . fn ( ( ) => Promise . resolve ( { data } ) ) ;
const api = { getRoot , isAuthenticated } ;
const wrapper = await main ( render , api ) ;
2018-11-02 20:45:11 +03:00
2019-01-02 10:28:24 +03:00
expect ( api . getRoot ) . toHaveBeenCalled ( ) ;
2019-01-07 15:26:21 +03:00
expect ( wrapper . find ( 'App' ) ) . toHaveLength ( 1 ) ;
expect ( wrapper . find ( 'Login' ) ) . toHaveLength ( 0 ) ;
wrapper . find ( 'header a' ) . simulate ( 'click' ) ;
wrapper . update ( ) ;
expect ( wrapper . find ( 'App' ) ) . toHaveLength ( 1 ) ;
2019-01-02 10:28:24 +03:00
expect ( wrapper . find ( 'Login' ) ) . toHaveLength ( 0 ) ;
2018-11-02 20:45:11 +03:00
2019-01-02 10:28:24 +03:00
done ( ) ;
2018-11-02 20:45:11 +03:00
} ) ;
2019-01-07 15:26:21 +03:00
test ( 'getLanguage returns the expected language code' , ( ) => {
expect ( getLanguage ( { languages : [ 'es-US' ] } ) ) . toEqual ( 'es' ) ;
expect ( getLanguage ( { languages : [ 'es-US' ] , language : 'fr-FR' , userLanguage : 'en-US' } ) ) . toEqual ( 'es' ) ;
expect ( getLanguage ( { language : 'fr-FR' , userLanguage : 'en-US' } ) ) . toEqual ( 'fr' ) ;
expect ( getLanguage ( { userLanguage : 'en-US' } ) ) . toEqual ( 'en' ) ;
} ) ;
2018-11-02 20:45:11 +03:00
} ) ;