This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 827
Tests for AdvancedRoomSettingsTab #9994
Merged
andybalaam
merged 6 commits into
matrix-org:develop
from
andybalaam:andybalaam/tests-for-AdvancedRoomSettingsTab
Jan 27, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ae89dd2
Sort the methods in mkStubRoom
andybalaam c02930a
Tests for AdvancedRoomSettingsTab
andybalaam 109196f
Fix noImplicitAny warnings
andybalaam 62a1235
Another attempt to do noImplicitAny
andybalaam be69fa6
Attempt 3 to fix noImplicitAny
andybalaam 06f5b99
Fix all noImplicitAny errors in test-utils.ts
andybalaam 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
104 changes: 104 additions & 0 deletions
104
test/components/views/settings/tabs/room/AdvancedRoomSettingsTab-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,104 @@ | ||
/* | ||
Copyright 2023 The Matrix.org Foundation C.I.C. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import React from "react"; | ||
import { fireEvent, render, RenderResult } from "@testing-library/react"; | ||
import { MatrixClient } from "matrix-js-sdk/src/client"; | ||
import { Room } from "matrix-js-sdk/src/models/room"; | ||
import { mocked } from "jest-mock"; | ||
import { EventType, MatrixEvent } from "matrix-js-sdk/src/matrix"; | ||
|
||
import AdvancedRoomSettingsTab from "../../../../../../src/components/views/settings/tabs/room/AdvancedRoomSettingsTab"; | ||
import { mkEvent, mkStubRoom, stubClient } from "../../../../../test-utils"; | ||
import dis from "../../../../../../src/dispatcher/dispatcher"; | ||
import { Action } from "../../../../../../src/dispatcher/actions"; | ||
import { MatrixClientPeg } from "../../../../../../src/MatrixClientPeg"; | ||
|
||
jest.mock("../../../../../../src/dispatcher/dispatcher"); | ||
|
||
describe("AdvancedRoomSettingsTab", () => { | ||
const roomId = "!room:example.com"; | ||
let cli: MatrixClient; | ||
let room: Room; | ||
|
||
const renderTab = (): RenderResult => { | ||
return render(<AdvancedRoomSettingsTab roomId={roomId} closeSettingsFn={jest.fn()} />); | ||
}; | ||
|
||
beforeEach(() => { | ||
stubClient(); | ||
cli = MatrixClientPeg.get(); | ||
room = mkStubRoom(roomId, "test room", cli); | ||
mocked(cli.getRoom).mockReturnValue(room); | ||
}); | ||
|
||
it("should render as expected", () => { | ||
const tab = renderTab(); | ||
expect(tab.asFragment()).toMatchSnapshot(); | ||
}); | ||
|
||
it("should display room ID", () => { | ||
const tab = renderTab(); | ||
tab.getByText(roomId); | ||
}); | ||
|
||
it("should display room version", () => { | ||
mocked(room.getVersion).mockReturnValue("custom_room_version_1"); | ||
|
||
const tab = renderTab(); | ||
tab.getByText("custom_room_version_1"); | ||
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. An |
||
}); | ||
|
||
function mockStateEvents(room: Room) { | ||
const createEvent = mkEvent({ | ||
event: true, | ||
user: "@a:b.com", | ||
type: EventType.RoomCreate, | ||
content: { predecessor: { room_id: "old_room_id", event_id: "tombstone_event_id" } }, | ||
room: room.roomId, | ||
}); | ||
|
||
type GetStateEvents2Args = (eventType: EventType | string, stateKey: string) => MatrixEvent | null; | ||
|
||
const getStateEvents = jest.spyOn( | ||
room.currentState, | ||
"getStateEvents", | ||
) as unknown as jest.MockedFunction<GetStateEvents2Args>; | ||
|
||
getStateEvents.mockImplementation((eventType: string | null, _key: string) => { | ||
switch (eventType) { | ||
case EventType.RoomCreate: | ||
return createEvent; | ||
default: | ||
return null; | ||
} | ||
}); | ||
} | ||
|
||
it("should link to predecessor room", async () => { | ||
mockStateEvents(room); | ||
const tab = renderTab(); | ||
const link = await tab.findByText("View older messages in test room."); | ||
fireEvent.click(link); | ||
expect(dis.dispatch).toHaveBeenCalledWith({ | ||
action: Action.ViewRoom, | ||
event_id: "tombstone_event_id", | ||
room_id: "old_room_id", | ||
metricsTrigger: "WebPredecessorSettings", | ||
metricsViaKeyboard: false, | ||
}); | ||
}); | ||
}); |
55 changes: 55 additions & 0 deletions
55
test/components/views/settings/tabs/room/__snapshots__/AdvancedRoomSettingsTab-test.tsx.snap
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,55 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`AdvancedRoomSettingsTab should render as expected 1`] = ` | ||
<DocumentFragment> | ||
<div | ||
class="mx_SettingsTab" | ||
> | ||
<div | ||
class="mx_SettingsTab_heading" | ||
> | ||
Advanced | ||
</div> | ||
<div | ||
class="mx_SettingsTab_section mx_SettingsTab_subsectionText" | ||
> | ||
<span | ||
class="mx_SettingsTab_subheading" | ||
> | ||
Room information | ||
</span> | ||
<div> | ||
<span> | ||
Internal room ID | ||
</span> | ||
<div | ||
class="mx_CopyableText mx_CopyableText_border" | ||
> | ||
!room:example.com | ||
<div | ||
aria-label="Copy" | ||
class="mx_AccessibleButton mx_CopyableText_copyButton" | ||
role="button" | ||
tabindex="0" | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
<div | ||
class="mx_SettingsTab_section mx_SettingsTab_subsectionText" | ||
> | ||
<span | ||
class="mx_SettingsTab_subheading" | ||
> | ||
Room version | ||
</span> | ||
<div> | ||
<span> | ||
Room version: | ||
</span> | ||
1 | ||
</div> | ||
</div> | ||
</div> | ||
</DocumentFragment> | ||
`; |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An
expect
is missing here ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test fails if
getByText
fails, so I think this is OK? Is there something I'm missing?