-
Notifications
You must be signed in to change notification settings - Fork 947
chore(site): clean up mocks after each test #12805
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3e544da
Restore all mocks after each test
BrunoQuaresma 8d561b7
Fix and improve location of useClipboard tests
BrunoQuaresma 30dd5c6
Fix terminal page tests
BrunoQuaresma 5f5e453
Mute react-query console.error logs
BrunoQuaresma 0ffaaf9
Fix act and warning errors on terminal page
BrunoQuaresma 0e73724
Fix fmt
BrunoQuaresma d9569cb
Refactor useEffect deps
BrunoQuaresma b1fb0b3
Rename terminal state to connection status
BrunoQuaresma 24cf212
Make the terminal a state
BrunoQuaresma 26e7a9d
Remove config.isLoading from deps
BrunoQuaresma 91abb66
Rollback config is loading
BrunoQuaresma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -8,35 +8,27 @@ import { | |
MockWorkspace, | ||
MockWorkspaceAgent, | ||
} from "testHelpers/entities"; | ||
import { | ||
renderWithAuth, | ||
waitForLoaderToBeRemoved, | ||
} from "testHelpers/renderHelpers"; | ||
import { renderWithAuth } from "testHelpers/renderHelpers"; | ||
import { server } from "testHelpers/server"; | ||
import TerminalPage, { Language } from "./TerminalPage"; | ||
|
||
Object.defineProperty(window, "matchMedia", { | ||
writable: true, | ||
value: jest.fn().mockImplementation((query) => ({ | ||
matches: false, | ||
media: query, | ||
onchange: null, | ||
addListener: jest.fn(), // deprecated | ||
removeListener: jest.fn(), // deprecated | ||
addEventListener: jest.fn(), | ||
removeEventListener: jest.fn(), | ||
dispatchEvent: jest.fn(), | ||
})), | ||
}); | ||
|
||
const renderTerminal = async ( | ||
route = `/${MockUser.username}/${MockWorkspace.name}/terminal`, | ||
) => { | ||
const utils = renderWithAuth(<TerminalPage />, { | ||
route, | ||
path: "/:username/:workspace/terminal", | ||
}); | ||
await waitForLoaderToBeRemoved(); | ||
await waitFor(() => { | ||
// To avoid 'act' errors during testing, we ensure the component is | ||
// completely rendered without any outstanding state updates. This is | ||
// accomplished by incorporating a 'data-status' attribute into the | ||
// component. We then observe this attribute for any changes, as we cannot | ||
// rely on other screen elements to indicate completion. | ||
const wrapper = | ||
utils.container.querySelector<HTMLDivElement>("[data-status]")!; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Big fan of the updated name |
||
expect(wrapper.dataset.state).not.toBe("initializing"); | ||
}); | ||
return utils; | ||
}; | ||
|
||
|
@@ -58,11 +50,15 @@ const expectTerminalText = (container: HTMLElement, text: string) => { | |
}; | ||
|
||
describe("TerminalPage", () => { | ||
afterEach(() => { | ||
WS.clean(); | ||
}); | ||
|
||
it("loads the right workspace data", async () => { | ||
const spy = jest | ||
jest | ||
.spyOn(API, "getWorkspaceByOwnerAndName") | ||
.mockResolvedValue(MockWorkspace); | ||
const ws = new WS( | ||
new WS( | ||
`ws://localhost/api/v2/workspaceagents/${MockWorkspaceAgent.id}/pty`, | ||
); | ||
await renderTerminal( | ||
|
@@ -75,57 +71,45 @@ describe("TerminalPage", () => { | |
{ include_deleted: true }, | ||
); | ||
}); | ||
spy.mockRestore(); | ||
ws.close(); | ||
}); | ||
|
||
it("shows an error if fetching workspace fails", async () => { | ||
// Given | ||
server.use( | ||
http.get("/api/v2/users/:userId/workspace/:workspaceName", () => { | ||
return HttpResponse.json({ id: "workspace-id" }, { status: 500 }); | ||
}), | ||
); | ||
|
||
// When | ||
const { container } = await renderTerminal(); | ||
|
||
// Then | ||
await expectTerminalText(container, Language.workspaceErrorMessagePrefix); | ||
}); | ||
|
||
it("shows an error if the websocket fails", async () => { | ||
// Given | ||
server.use( | ||
http.get("/api/v2/workspaceagents/:agentId/pty", () => { | ||
return HttpResponse.json({}, { status: 500 }); | ||
}), | ||
); | ||
|
||
// When | ||
const { container } = await renderTerminal(); | ||
|
||
// Then | ||
await expectTerminalText(container, Language.websocketErrorMessagePrefix); | ||
}); | ||
|
||
it("renders data from the backend", async () => { | ||
// Given | ||
const ws = new WS( | ||
`ws://localhost/api/v2/workspaceagents/${MockWorkspaceAgent.id}/pty`, | ||
); | ||
const text = "something to render"; | ||
|
||
// When | ||
const { container } = await renderTerminal(); | ||
|
||
// Then | ||
// Ideally we could use ws.connected but that seems to pause React updates. | ||
// For now, wait for the initial resize message instead. | ||
await ws.nextMessage; | ||
ws.send(text); | ||
|
||
await expectTerminalText(container, text); | ||
ws.close(); | ||
}); | ||
|
||
// Ideally we could just pass the correct size in the web socket URL without | ||
|
@@ -134,40 +118,32 @@ describe("TerminalPage", () => { | |
// in the other tests since ws.connected appears to pause React updates. So | ||
// for now the initial resize message (and this test) are here to stay. | ||
it("resizes on connect", async () => { | ||
// Given | ||
const ws = new WS( | ||
`ws://localhost/api/v2/workspaceagents/${MockWorkspaceAgent.id}/pty`, | ||
); | ||
|
||
// When | ||
await renderTerminal(); | ||
|
||
// Then | ||
const msg = await ws.nextMessage; | ||
const req = JSON.parse(new TextDecoder().decode(msg as Uint8Array)); | ||
expect(req.height).toBeGreaterThan(0); | ||
expect(req.width).toBeGreaterThan(0); | ||
ws.close(); | ||
}); | ||
|
||
it("supports workspace.agent syntax", async () => { | ||
// Given | ||
const ws = new WS( | ||
`ws://localhost/api/v2/workspaceagents/${MockWorkspaceAgent.id}/pty`, | ||
); | ||
const text = "something to render"; | ||
|
||
// When | ||
const { container } = await renderTerminal( | ||
`/some-user/${MockWorkspace.name}.${MockWorkspaceAgent.name}/terminal`, | ||
); | ||
|
||
// Then | ||
// Ideally we could use ws.connected but that seems to pause React updates. | ||
// For now, wait for the initial resize message instead. | ||
await ws.nextMessage; | ||
ws.send(text); | ||
await expectTerminalText(container, text); | ||
ws.close(); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.