11import React , { useContext , useState } from 'react' ;
22import { create , act } from 'react-test-renderer' ;
3+ import { render , within , fireEvent , waitFor , screen } from '@testing-library/react' ;
4+
35
46import {
57 Gateway ,
@@ -8,35 +10,109 @@ import {
810} from '../src/index.js' ;
911
1012
11- const withExposedSetState = ( Component ) => {
12- let setState ;
13- let setSetState = ( setStateParam ) => {
14- setState = setStateParam ;
15- } ;
16- return {
17- setState : ( state ) => {
18- setState ( state ) ;
19- } ,
20- Component : ( props ) => < Component setSetState = { setSetState } { ...props } />
21- } ;
22- } ;
23-
13+ import { withExposedSetState } from './utils.js' ;
2414
2515describe ( 'Gateway' , function ( ) {
26- it ( 'throws on invalid dest name' , function ( ) {
27- const throwingRender = ( ) => {
28- act ( ( ) => {
29- create (
16+ describe ( 'verification' , ( ) => {
17+ beforeEach ( ( ) => {
18+ jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
19+ } ) ;
20+ it ( 'throws on invalid dest name' , function ( ) {
21+ const throwingRender = ( ) => {
22+ act ( ( ) => {
23+ create (
24+ < GatewayProvider >
25+ < div >
26+ < GatewayDest name = "invaid##" />
27+ < Gateway into = "invaid##" />
28+ </ div >
29+ </ GatewayProvider >
30+ ) ;
31+ } ) ;
32+ } ;
33+ expect ( throwingRender ) . toThrow ( 'dest names should not have ##' ) ;
34+ } ) ;
35+ } ) ;
36+
37+ describe ( 'sorting' , ( ) => {
38+ beforeEach ( ( ) => {
39+ jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
40+ } ) ;
41+
42+ it ( 'should render in natural order' , ( ) => {
43+ render (
44+ < GatewayProvider >
45+ < div >
46+ < section >
47+ < Gateway into = "foo" > First </ Gateway >
48+ < Gateway into = "foo" > Second </ Gateway >
49+ </ section >
50+ < GatewayDest name = "foo" data-testid = "mydiv" />
51+ </ div >
52+ </ GatewayProvider >
53+ ) ;
54+ const textContent = screen . getByTestId ( 'mydiv' ) . textContent ;
55+ expect ( textContent ) . toContain ( 'First Second' ) ;
56+ } ) ;
57+ it ( 'should render by sort prop' , ( ) => {
58+ let rendered = render (
59+ < GatewayProvider >
60+ < div >
61+ < section >
62+ < Gateway into = "foo" sort = { 2 } > Second </ Gateway >
63+ < Gateway into = "foo" sort = { 1 } > First </ Gateway >
64+ </ section >
65+ < GatewayDest name = "foo" data-testid = "mydiv" />
66+ </ div >
67+ </ GatewayProvider >
68+ ) ;
69+ const textContent = screen . getByTestId ( 'mydiv' ) . textContent ;
70+ expect ( textContent ) . toContain ( 'First Second' ) ;
71+ } ) ;
72+ it ( 'should render undefined sort prop last' , ( ) => {
73+ render (
3074 < GatewayProvider >
3175 < div >
32- < GatewayDest name = "invaid##" />
33- < Gateway into = "invaid##" />
76+ < section >
77+ < Gateway into = "foo" > Third </ Gateway >
78+ < Gateway into = "foo" sort = { 2 } > Second </ Gateway >
79+ < Gateway into = "foo" sort = { 1 } > First </ Gateway >
80+ </ section >
81+ < GatewayDest name = "foo" data-testid = "mydiv" />
3482 </ div >
3583 </ GatewayProvider >
3684 ) ;
37- } ) ;
38- } ;
39- expect ( throwingRender ) . toThrow ( 'dest names should not have ##' ) ;
85+ const textContent = screen . getByTestId ( 'mydiv' ) . textContent ;
86+ expect ( textContent ) . toContain ( 'First Second Third' ) ;
87+ } ) ;
88+ it ( 'should render undefined sort prop in natural order' , ( ) => {
89+ render (
90+ < GatewayProvider >
91+ < div >
92+ < section >
93+ < Gateway into = "foo" > Second </ Gateway >
94+ < Gateway into = "foo" > Third </ Gateway >
95+ < Gateway into = "foo" sort = { 3 } > First </ Gateway >
96+ </ section >
97+ < GatewayDest name = "foo" data-testid = "mydiv" />
98+ </ div >
99+ </ GatewayProvider >
100+ ) ;
101+ const textContent = screen . getByTestId ( 'mydiv' ) . textContent ;
102+ expect ( textContent ) . toContain ( 'First Second Third' ) ;
103+ } ) ;
104+ it ( 'should validate sort to be a number only' , ( ) => {
105+ const throwingRender = ( ) => {
106+ act ( ( ) => {
107+ create (
108+ < GatewayProvider >
109+ < Gateway into = "invalid" sort = { ( ) => { } } />
110+ </ GatewayProvider >
111+ ) ;
112+ } ) ;
113+ } ;
114+ expect ( throwingRender ) . toThrow ( 'sort should be a number' ) ;
115+ } ) ;
40116 } ) ;
41117
42118 it ( 'should render Gateway in GatewayDest' , function ( ) {
0 commit comments