|
1 | 1 | importReactfrom"react";
|
2 | 2 | import{render}from"@testing-library/react";
|
3 | 3 | import{waitForElementToBeRemoved}from"@testing-library/dom";
|
4 |
| -import{Feed}from"../../components/Feed"; |
| 4 | +import{renderHook}from"@testing-library/react-hooks"; |
| 5 | +import{Feed,useApi}from"../../components/Feed"; |
5 | 6 |
|
6 | 7 | constmockData=[
|
7 | 8 | {
|
@@ -45,3 +46,36 @@ describe("Feed", () => {
|
45 | 46 | expect(feed).toBeDefined();
|
46 | 47 | });
|
47 | 48 | });
|
| 49 | + |
| 50 | +// Hook |
| 51 | + |
| 52 | +describe("useApi",()=>{ |
| 53 | +test("should initialize with an error null",async()=>{ |
| 54 | +const{ result, waitForNextUpdate}=renderHook(()=>useApi()); |
| 55 | +awaitwaitForNextUpdate(); |
| 56 | +expect(result.current.error).toBe(null); |
| 57 | +}); |
| 58 | + |
| 59 | +// generated useEffect async warnings |
| 60 | +test("should initialize with loading true",async()=>{ |
| 61 | +const{ result}=renderHook(()=>useApi()); |
| 62 | +expect(result.current.loading).toBe(true); |
| 63 | +}); |
| 64 | + |
| 65 | +test("should change loading to true on update",async()=>{ |
| 66 | +const{ result, waitForNextUpdate}=renderHook(()=>useApi()); |
| 67 | +awaitwaitForNextUpdate(); |
| 68 | +expect(result.current.loading).toBe(false); |
| 69 | +}); |
| 70 | + |
| 71 | +test("should initialize with data null",()=>{ |
| 72 | +const{ result}=renderHook(()=>useApi()); |
| 73 | +expect(result.current.data).toBe(null); |
| 74 | +}); |
| 75 | + |
| 76 | +test("should return data once api completes",async()=>{ |
| 77 | +const{ result, waitForNextUpdate}=renderHook(()=>useApi()); |
| 78 | +awaitwaitForNextUpdate(); |
| 79 | +expect(result.current.data.length).toBe(1); |
| 80 | +}); |
| 81 | +}); |