1
- import { fireEvent , render , screen } from '@testing-library/react' ;
1
+ import { fireEvent , getByTestId , render , screen , waitFor , debug } from '@testing-library/react' ;
2
2
import App from './App' ;
3
- import { create } from 'react-test-renderer'
4
3
5
- describe ( 'My first snapshot test ' , ( ) => {
6
- test ( 'testing app' , ( ) => {
7
- let tree = create ( < App /> ) ;
8
- expect ( tree . toJSON ( ) ) . toMatchSnapshot ( ) ;
9
- } )
10
- } ) ;
11
- describe ( 'Inputting a text file' , ( ) => {
12
- // test('input textfile', () => {
13
- // // const component = render(<App /> );
14
-
15
- // // fireEvent.click (screen.getByText('Upload File') );
16
- // } );
17
- test ( 'handleFiles' , ( ) => {
18
-
4
+ describe ( 'App ' , ( ) => {
5
+
6
+ test ( 'renders App Component' , async ( ) => {
7
+ render ( < App /> ) ;
8
+ expect ( screen . getByText ( / U p l o a d a f i l e t o r u n W o r d S t a t i s t i c s / ) ) . toBeInTheDocument ( ) ;
9
+ expect ( screen . getByText ( / e m p t y / ) ) . toBeInTheDocument ( ) ;
10
+ expect ( screen . getByText ( / W o r d C o u n t : / ) ) . toBeInTheDocument ( ) ;
11
+ expect ( screen . getByText ( / W o r d F r e q u e n c y : / ) ) . toBeInTheDocument ( ) ;
12
+ expect ( screen . getByText ( / L i n e C o u n t : / ) ) . toBeInTheDocument ( ) ;
13
+ expect ( screen . getByText ( / C h a r C o u n t : / ) ) . toBeInTheDocument ( ) ;
14
+ expect ( screen . getByText ( / U n i q u e C h a r F r e q u e n c y : / ) ) . toBeInTheDocument ( ) ;
15
+ expect ( screen . getByText ( / W o r d R e p l a c e m e n t : / ) ) . toBeInTheDocument ( ) ;
16
+ expect ( screen . getByText ( / W o r d t o b e r e p l a c e d : / ) ) . toBeInTheDocument ( ) ;
17
+ expect ( screen . getByText ( / R e p l a c e m e n t f o r s a i d w o r d : / ) ) . toBeInTheDocument ( ) ;
19
18
} ) ;
20
- test ( 'handleSubmit' , ( ) => {
21
19
20
+ test ( 'handleFiles' , async ( ) => {
21
+ let app = render ( < App /> ) ;
22
+
23
+ const file = new File ( [ 'a bb ccc' ] , 'data.txt' , { type : "text/plain;charset=utf-8" } ) ;
24
+ const handleFileUploadMockFn = jest . fn ( ) ;
25
+
26
+ await waitFor ( ( ) =>
27
+ fireEvent . change ( app . getByTestId ( 'upload-input' ) , {
28
+ target : { files : [ file ] } ,
29
+ } ) ) ;
30
+
31
+ let textfile = app . getAllByTestId ( 'file-name' ) . have
32
+ expect ( screen . getByText ( / F i l e u p l o a d e d : d a t a .t x t / ) ) . toBeInTheDocument ( ) ;
33
+
34
+ await waitFor ( ( ) => {
35
+ expect ( handleFileUploadMockFn ) . not . toHaveBeenCalled ( ) ;
36
+ }
37
+ ) ;
22
38
} ) ;
23
- test ( 'has correct body' , ( ) => {
24
- render ( < App /> ) ;
25
- let headertwo = [ < h2 title = "word-count" > Word Count: </ h2 > , < h2 > Word Frequency: </ h2 > , < h2 > Line Count: </ h2 > , < h2 > Char Count: </ h2 > , < h2 > Unique Char Frequency: </ h2 > , < h2 > Word Replacement: </ h2 > ]
26
- // let wordCountHeader = <h2 t)
27
- // expect(screen.getByRole('heading', { level: 4 })).toHaveTextContent('Created by: Sergio J Falcon');
28
- // expect(screen.getByRole('heading', { level: 1 })).toHaveTextContent('Upload a file to run Word Statistics');
29
- // expect(screen.queryAllByRole('heading', { level: 2 })).toHaveTextContent(headertwo);
30
- // console.log(getByText('Word Count: ', 'h2'))
31
- //expect(screen.getAllByTitle('word-count')).toHaveTextContent('Word Count: ');
39
+ test ( 'wrong file for fileReader' , ( ) => {
40
+ let app = render ( < App /> ) ;
41
+ const file = new File ( [ ] , '' ) ;
42
+ const mTxt = new Blob ( [ "text inside this file" ] , { type : "text/plain;charset=utf-8" } )
43
+
44
+ const mEvent = { target : { files : [ file ] } } ;
32
45
33
46
} )
47
+ test ( 'handleSubmit' , ( ) => {
48
+ const mockSubmit = jest . fn ( ( ) => {
49
+ console . log ( "onSubmit" ) ;
50
+ } ) ;
51
+ const { getByTestId } = render ( < App handleSubmit = { mockSubmit } /> ) ;
52
+
53
+ fireEvent . change ( getByTestId ( "word-before-input" ) , { target : { value : 'a' } } ) ;
54
+ fireEvent . change ( getByTestId ( "new-word" , { target : { value : 'one' } } ) ) ;
55
+ fireEvent . click ( getByTestId ( "enter-input" ) ) ;
56
+ fireEvent . submit ( getByTestId ( "form" ) ) ;
57
+
58
+ expect ( mockSubmit ) . not . toHaveBeenCalled ( ) ;
59
+ } ) ;
34
60
} ) ;
0 commit comments