File tree 3 files changed +33
-17
lines changed
3 files changed +33
-17
lines changed Original file line number Diff line number Diff line change 1
1
<template >
2
- <div >
3
- <div v-if =" loading" >Loading...</div >
4
- <div v-else data-testid =" message" >
5
- Loaded this message: {{ data.returnedMessage }}
6
- </div >
7
- </div >
2
+ <p v-if =" loading" >Loading...</p >
3
+ <p v-else data-testid =" message" >
4
+ Loaded this message: {{ data.returnedMessage }}
5
+ </p >
8
6
</template >
9
7
10
8
<script >
@@ -13,6 +11,7 @@ const fetchAMessage = () =>
13
11
// we are using random timeout here to fireEvent a real-time example
14
12
// of an async operation calling a callback at a non-deterministic time
15
13
const randomTimeout = Math .floor (Math .random () * 100 )
14
+
16
15
setTimeout (() => {
17
16
resolve ({ returnedMessage: ' Hello World' })
18
17
}, randomTimeout)
Original file line number Diff line number Diff line change
1
+ import { render , waitForElementToBeRemoved } from '@testing-library/vue'
2
+ import Disappearance from './components/Disappearance'
3
+ import 'jest-dom/extend-expect'
4
+
5
+ test ( 'it waits for the data to be loaded' , async ( ) => {
6
+ const { getByText, queryByText, queryByTestId } = render ( Disappearance )
7
+
8
+ // Assert initial state
9
+ getByText ( 'Loading...' )
10
+ expect ( queryByText ( / L o a d e d t h i s m e s s a g e / ) ) . not . toBeInTheDocument ( )
11
+
12
+ // Line reads as follows "Wait until element with test 'Loading...' is gone."
13
+ await waitForElementToBeRemoved ( ( ) => queryByText ( 'Loading...' ) )
14
+ // It is equivalent to:
15
+ //
16
+ // await wait(() => {
17
+ // expect(queryByText('Loading...')).not.toBeInTheDocument()
18
+ // })
19
+ //
20
+ // `wait()` waits until the callback function passes or times out.
21
+
22
+ // After 'Loading...' element is gone, we can assert that fetched data is
23
+ // rendered.
24
+ expect ( queryByTestId ( 'message' ) ) . toHaveTextContent ( / H e l l o W o r l d / )
25
+
26
+ // Read more about async utilities:
27
+ // https://testing-library.com/docs/dom-testing-library/api-async
28
+ } )
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments