-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(room_access_link): add test for room access by link
- Loading branch information
marc.sirisak
committed
Nov 6, 2024
1 parent
db2cd18
commit 7e77bfe
Showing
5 changed files
with
272 additions
and
53 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
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
165 changes: 165 additions & 0 deletions
165
test/unit-tests/tchap/components/views/rooms/TchapRoomLinkAccess-test.tsx
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,165 @@ | ||
import React from "react"; | ||
import { render, screen, waitFor, waitForElementToBeRemoved } from "@testing-library/react"; | ||
import { mocked } from "jest-mock"; | ||
import { mkStubRoom, stubClient } from "matrix-react-sdk/test/test-utils/test-utils"; | ||
import { EventType, GuestAccess, JoinRule, MatrixClient, Room } from "matrix-js-sdk/src/matrix"; | ||
import { makeRoomPermalink } from "matrix-react-sdk/src/utils/permalinks/Permalinks"; | ||
import userEvent from "@testing-library/user-event"; | ||
|
||
import TchapRoomLinkAccess from "../../../../../../src/tchap/components/views/rooms/TchapRoomLinkAccess"; | ||
import { TchapRoomType } from "../../../../../../src/tchap/@types/tchap"; | ||
|
||
import TchapRoomUtils from "~tchap-web/src/tchap/util/TchapRoomUtils"; | ||
import { | ||
flushPromises, | ||
waitEnoughCyclesForModal, | ||
} from "~tchap-web/linked-dependencies/matrix-react-sdk/test/test-utils"; | ||
|
||
jest.mock("~tchap-web/src/tchap/util/TchapRoomUtils"); | ||
jest.mock("matrix-react-sdk/src/utils/permalinks/Permalinks"); | ||
|
||
describe("TchapRoomLinkAccess", () => { | ||
const client: MatrixClient = stubClient(); | ||
const room: Room = mkStubRoom("roomId", "test", client); | ||
|
||
const mockedTchapRoomUtils = mocked(TchapRoomUtils); | ||
const mockedMakeRoomPermalink = mocked(makeRoomPermalink); | ||
const onUpdateParentView = jest.fn().mockImplementation(() => {}); | ||
|
||
const mockedLinked = "https://testmocked.matrix.org"; | ||
|
||
const getComponent = () => render(<TchapRoomLinkAccess room={room} onUpdateParentView={onUpdateParentView} />); | ||
|
||
beforeEach(() => { | ||
mockedTchapRoomUtils.getTchapRoomType.mockImplementation(() => TchapRoomType.Private); | ||
mockedTchapRoomUtils.getRoomJoinRule.mockImplementation(() => JoinRule.Invite); | ||
mockedTchapRoomUtils.isUserAdmin.mockImplementation(() => true); | ||
mockedTchapRoomUtils.getRoomGuessAccessRule.mockImplementation(() => GuestAccess.CanJoin); | ||
mockedMakeRoomPermalink.mockImplementation(() => mockedLinked); | ||
|
||
client.createAlias = jest.fn().mockResolvedValue("alias"); | ||
|
||
jest.spyOn(client, "sendStateEvent").mockResolvedValue(Promise.resolve({ event_id: "" })); | ||
jest.spyOn(client, "sendStateEvent").mockResolvedValue(Promise.resolve({ event_id: "" })); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should render correct initial value when joinrule is public", async () => { | ||
mockedTchapRoomUtils.getRoomJoinRule.mockImplementation(() => JoinRule.Public); | ||
mockedTchapRoomUtils.getTchapRoomType.mockImplementation(() => TchapRoomType.Forum); | ||
|
||
getComponent(); | ||
|
||
await flushPromises(); | ||
|
||
const switchLink = screen.queryByRole("switch"); | ||
|
||
const linkDisplay = screen.queryByText(mockedLinked); | ||
|
||
expect(linkDisplay).toBeDefined(); | ||
|
||
// should be disable because we dont change this settings for forum room | ||
expect(switchLink).toHaveAttribute("aria-disabled", "true"); | ||
}); | ||
|
||
it("should render correct initial value when joinrule is invite", async () => { | ||
mockedTchapRoomUtils.getRoomJoinRule.mockImplementation(() => JoinRule.Invite); | ||
|
||
getComponent(); | ||
|
||
await flushPromises(); | ||
|
||
const switchLink = screen.queryByRole("switch"); | ||
|
||
const linkDisplay = screen.queryByText(mockedLinked); | ||
|
||
// linked should not appear because the share link is deactivated | ||
expect(linkDisplay).toBe(null); | ||
|
||
// we should be able to click on the link to activate it | ||
expect(switchLink).toHaveAttribute("aria-disabled", "false"); | ||
}); | ||
|
||
it("should disable link if user is not admin", async () => { | ||
mockedTchapRoomUtils.getRoomJoinRule.mockImplementation(() => JoinRule.Invite); | ||
mockedTchapRoomUtils.isUserAdmin.mockImplementation(() => false); | ||
|
||
getComponent(); | ||
|
||
await flushPromises(); | ||
|
||
const switchLink = screen.queryByRole("switch"); | ||
|
||
const linkDisplay = screen.queryByText(mockedLinked); | ||
|
||
// linked should not appear because the share link is deactivated | ||
expect(linkDisplay).toBe(null); | ||
|
||
// the user should not be able to click on the button | ||
expect(switchLink).toHaveAttribute("aria-disabled", "true"); | ||
}); | ||
|
||
it("should activate link when clicking on the switch", async () => { | ||
// jest.useFakeTimers(); | ||
|
||
mockedTchapRoomUtils.getRoomJoinRule.mockImplementation(() => JoinRule.Invite); | ||
getComponent(); | ||
|
||
await flushPromises(); | ||
|
||
const switchLink = screen.getByRole("switch"); | ||
|
||
// should open dialog | ||
userEvent.click(switchLink); | ||
|
||
await waitEnoughCyclesForModal({ | ||
useFakeTimers: true, | ||
}); | ||
|
||
const dialog = await screen.findByRole("dialog"); | ||
|
||
expect(dialog).toMatchSnapshot(); | ||
|
||
const confirmButton = screen.getByTestId("dialog-primary-button"); | ||
|
||
userEvent.click(confirmButton); | ||
|
||
await waitForElementToBeRemoved(dialog); | ||
// should activate the switch with public join rule value | ||
expect(mockedMakeRoomPermalink).toHaveBeenCalledTimes(1); | ||
// joinrule to public, guest access to forbiden and canonical alias | ||
expect(room.client.sendStateEvent).toHaveBeenCalledTimes(3); | ||
expect(switchLink).toHaveAttribute("aria-checked", "true"); | ||
}); | ||
|
||
it("should deactivate link when clicking on the switch", async () => { | ||
mockedTchapRoomUtils.getRoomJoinRule.mockImplementation(() => JoinRule.Public); | ||
|
||
getComponent(); | ||
|
||
await flushPromises(); | ||
|
||
const switchLink = screen.getByRole("switch"); | ||
|
||
userEvent.click(switchLink); | ||
|
||
await waitFor(() => { | ||
const linkDisplay = screen.queryByText(mockedLinked); | ||
|
||
// no link since we deactivated the switch | ||
expect(linkDisplay).toBe(null); | ||
// should deactivate the switch | ||
expect(room.client.sendStateEvent).toHaveBeenCalledTimes(1); | ||
expect(room.client.sendStateEvent).toHaveBeenCalledWith( | ||
room.roomId, | ||
EventType.RoomJoinRules, | ||
{ join_rule: JoinRule.Invite }, | ||
"", | ||
); | ||
expect(switchLink).toHaveAttribute("aria-checked", "false"); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.