1
- import 'react-native'
2
1
import React from 'react'
3
- import { act , fireEvent , render } from '@testing-library/react-native'
2
+ import {
3
+ act ,
4
+ cleanup ,
5
+ fireEvent ,
6
+ render ,
7
+ screen ,
8
+ } from '@testing-library/react-native'
4
9
import CounterUsesCustomHook from '../src/components/CounterUsesCustomHook'
5
10
import useCounter from '../src/hooks/useCounter'
6
11
import { renderHook } from '@testing-library/react-hooks'
7
- import { expect , it , test } from '@jest/globals'
8
12
9
- //testing with the component
13
+ afterEach ( cleanup )
14
+
10
15
it ( 'exposes the count and increment/decrement functions' , ( ) => {
11
- const { getByText} = render ( < CounterUsesCustomHook /> )
16
+ render ( < CounterUsesCustomHook /> )
17
+ const { getByText} = screen
12
18
13
19
const decrement = getByText ( / d e c r e m e n t / i)
14
20
const increment = getByText ( / i n c r e m e n t / i)
@@ -22,18 +28,17 @@ it('exposes the count and increment/decrement functions', () => {
22
28
} )
23
29
24
30
// @ts -ignore
25
- function setup ( { initialProps} = { } ) {
31
+ const setup = ( { initialProps} = { } ) => {
26
32
const result : any = { current : null }
27
- function TestComponent ( props : any ) {
33
+ const TestComponent = ( props : any ) => {
28
34
result . current = useCounter ( props )
29
35
return null
30
36
}
31
37
render ( < TestComponent { ...initialProps } /> )
32
38
return result
33
39
}
34
40
35
- //testing without component
36
- test ( 'exposes the count and increment/decrement functions' , ( ) => {
41
+ it ( 'exposes the count and increment/decrement functions- without component' , ( ) => {
37
42
const result = setup ( )
38
43
expect ( result . current . count ) . toBe ( 0 )
39
44
act ( ( ) => result . current . increment ( ) )
@@ -42,12 +47,12 @@ test('exposes the count and increment/decrement functions', () => {
42
47
expect ( result . current . count ) . toBe ( 0 )
43
48
} )
44
49
45
- test ( 'allows customization of the initial count' , ( ) => {
50
+ it ( 'allows customization of the initial count' , ( ) => {
46
51
const result = setup ( { initialProps : { initialCount : 3 } } )
47
52
expect ( result . current . count ) . toBe ( 3 )
48
53
} )
49
54
50
- test ( 'allows customization of the step' , ( ) => {
55
+ it ( 'allows customization of the step' , ( ) => {
51
56
const result = setup ( { initialProps : { step : 2 } } )
52
57
expect ( result . current . count ) . toBe ( 0 )
53
58
act ( ( ) => result . current . increment ( ) )
@@ -56,7 +61,7 @@ test('allows customization of the step', () => {
56
61
expect ( result . current . count ) . toBe ( 0 )
57
62
} )
58
63
59
- test ( 'exposes the count and increment/decrement functions' , ( ) => {
64
+ it ( 'exposes the count and increment/decrement functions- hook only ' , ( ) => {
60
65
const { result} = renderHook ( useCounter )
61
66
expect ( result . current . count ) . toBe ( 0 )
62
67
act ( ( ) => result . current . increment ( ) )
0 commit comments