|
| 1 | +import React from 'react'; |
| 2 | +import ReactDOM, {render, unmountComponentAtNode} from 'react-dom'; |
| 3 | +import DropAndFetch from './dropAndFetch'; |
| 4 | +import { act } from "react-dom/test-utils"; |
| 5 | +import { constant } from 'lodash'; |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +describe('pageOne dropAndFetch: rendering correctly', () =>{ |
| 11 | + |
| 12 | + let container = null; |
| 13 | + beforeEach(() => { |
| 14 | + // setup a DOM element as a render target |
| 15 | + container = document.createElement("div"); |
| 16 | + document.body.appendChild(container); |
| 17 | + }); |
| 18 | + |
| 19 | + afterEach(() => { |
| 20 | + // cleanup on exiting |
| 21 | + unmountComponentAtNode(container); |
| 22 | + container.remove(); |
| 23 | + container = null; |
| 24 | + }); |
| 25 | + // Unit test |
| 26 | + it("DropAndFetch component renders correctly ", () => { |
| 27 | + ReactDOM.render(<DropAndFetch |
| 28 | + />, container); |
| 29 | + console.log(container.textContent); |
| 30 | + expect(container.textContent).toMatch('Upload Files'); |
| 31 | + }); |
| 32 | + // Integration test |
| 33 | + it("DropZone component inside DropAndFetch renders correctly", () =>{ |
| 34 | + ReactDOM.render(<DropAndFetch |
| 35 | + />, container); |
| 36 | + //Renders dropzone icon |
| 37 | + expect(container.querySelector("svg")).toBeInTheDocument(); |
| 38 | + }); |
| 39 | +}) |
| 40 | + |
| 41 | +describe('pageOne dropAndFetch: fetch correctly', () =>{ |
| 42 | + let container = null; |
| 43 | + beforeEach(() => { |
| 44 | + // setup a DOM element as a render target |
| 45 | + container = document.createElement("div"); |
| 46 | + document.body.appendChild(container); |
| 47 | + //fetch.resetMocks(); |
| 48 | + }); |
| 49 | + |
| 50 | + afterEach(() => { |
| 51 | + // cleanup on exiting |
| 52 | + unmountComponentAtNode(container); |
| 53 | + container.remove(); |
| 54 | + container = null; |
| 55 | + }); |
| 56 | + |
| 57 | + |
| 58 | + it('UploadPDDL fetch works correctly', async () =>{ |
| 59 | + const fakevfg = {vfg: 'correct'}; |
| 60 | + global.fetch = jest.fn(() => |
| 61 | + Promise.resolve({ |
| 62 | + json: () => Promise.resolve(fakevfg) |
| 63 | + }) |
| 64 | + ); |
| 65 | + |
| 66 | + React.useState = jest.fn() |
| 67 | + .mockReturnValueOnce([{domain: '',problem:'',animation:''},{}]) |
| 68 | + .mockReturnValueOnce([false, ()=>{}]) |
| 69 | + .mockReturnValueOnce([false, ()=>{}]); |
| 70 | + |
| 71 | + const handleStore = (content) => { |
| 72 | + expect(content).toMatch(JSON.stringify({vfg: 'correct'})); |
| 73 | + } |
| 74 | + |
| 75 | + // Use the asynchronous version of act to apply resolved promises |
| 76 | + await act(async () => { |
| 77 | + render(<DropAndFetch onStore={handleStore} newURL={''}/>, container); |
| 78 | + }); |
| 79 | + |
| 80 | + container.querySelector("button[class~='MuiButton-containedPrimary']").click(); |
| 81 | + global.fetch.mockRestore(); |
| 82 | + }) |
| 83 | +}) |
| 84 | + |
| 85 | + |
0 commit comments