-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Challe-P/testy-tests
Testy tests
- Loading branch information
Showing
5 changed files
with
208 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import React from 'react'; | ||
import { render, screen, fireEvent, waitFor, act } from '@testing-library/react'; | ||
import { getOne } from "../models/fetch.js"; | ||
import { useParams } from 'react-router-dom'; | ||
import UpdateDoc from '../update-doc.jsx'; | ||
import { runCode } from "../models/exec"; | ||
|
||
// Mock the navigate function for delete function | ||
jest.mock('react-router-dom', () => ({ | ||
useParams: jest.fn(), | ||
useNavigate: jest.fn(), | ||
})); | ||
|
||
// Mock the getOne function | ||
jest.mock("../models/fetch", () => ({ | ||
getOne: jest.fn() | ||
})); | ||
|
||
jest.mock("../models/exec", () => ({ | ||
runCode: jest.fn() | ||
})); | ||
|
||
describe('Tests code editor', () => { | ||
|
||
beforeAll(() => { | ||
jest.spyOn(console, 'error').mockImplementation(jest.fn()); | ||
}); | ||
|
||
afterAll(() => { | ||
global.console.error.mockRestore(); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('tests that the run code function gets called correctly', async () => { | ||
useParams.mockReturnValue({id: "091823901283"}); | ||
const mockResponse = {title: "Fake title", content: "console.log('Test')", mode: "code"}; | ||
getOne.mockResolvedValue(mockResponse); | ||
runCode.mockReturnValue("Test"); | ||
|
||
await act(async () => { | ||
render(<UpdateDoc />); | ||
}); | ||
|
||
await act(async () => { | ||
fireEvent.click(screen.getByRole('button', { name: /Run code/i })); | ||
}); | ||
|
||
expect(runCode).toHaveBeenCalledWith("console.log('Test')"); | ||
const codeOutput = document.getElementsByClassName('code-output')[0]; | ||
expect(codeOutput).toBeInTheDocument; | ||
expect(codeOutput.textContent).toBe("Test"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { runCode } from "../models/exec"; | ||
|
||
// Mocking fetch to test module without network connections | ||
global.fetch = jest.fn(); | ||
|
||
describe('Test suite for exec module', () => { | ||
beforeEach(() => { | ||
fetch.mockClear(); | ||
}); | ||
|
||
test('Testing that runCode gets called correctly.', async () => { | ||
const code = "console.log('Test')"; | ||
const mockedResult = {data: btoa('Test')} | ||
const sentData = {code: btoa(code)}; | ||
|
||
fetch.mockResolvedValueOnce({ | ||
json: jest.fn().mockResolvedValue(mockedResult) | ||
}); | ||
const response = await runCode(code); | ||
|
||
expect(fetch).toHaveBeenCalledWith("https://execjs.emilfolino.se/code", { | ||
body: JSON.stringify(sentData), | ||
headers: { 'content-type': 'application/json' }, | ||
method: 'POST', | ||
}); | ||
|
||
expect(response).toEqual("Test") | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import React from 'react'; | ||
import { render, screen, fireEvent, waitFor, act } from '@testing-library/react'; | ||
import { getOne } from "../models/fetch.js"; | ||
import { useParams } from 'react-router-dom'; | ||
import UpdateDoc from '../update-doc.jsx'; | ||
import { runCode } from "../models/exec"; | ||
import userEvent from "@testing-library/user-event"; | ||
import Delta from 'quill-delta'; | ||
|
||
// Mock the navigate function for delete function | ||
jest.mock('react-router-dom', () => ({ | ||
useParams: jest.fn(), | ||
useNavigate: jest.fn(), | ||
})); | ||
|
||
// Mock the getOne function | ||
jest.mock("../models/fetch", () => ({ | ||
getOne: jest.fn() | ||
})); | ||
|
||
jest.mock("../models/exec", () => ({ | ||
runCode: jest.fn() | ||
})); | ||
|
||
describe('Tests quill editor', () => { | ||
|
||
beforeAll(() => { | ||
jest.spyOn(console, 'error').mockImplementation(jest.fn()); | ||
}); | ||
|
||
afterAll(() => { | ||
global.console.error.mockRestore(); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('tests that loading document with delta content works', async () => { | ||
useParams.mockReturnValue({id: "091823901283"}); | ||
const delta = new Delta({ops: [{"insert": "Fake delta content "}]}) | ||
const mockResponse = {title: "Fake title", content: delta, mode: "text"}; | ||
getOne.mockResolvedValue(mockResponse); | ||
await act(async () => { | ||
render(<UpdateDoc />); | ||
}); | ||
|
||
expect(screen.getByLabelText('Title')).toHaveValue('Fake title'); | ||
expect(document.getElementsByClassName('ql-editor')[0].textContent).toContain('Fake delta content'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters