From 79cf69bedb776175c3c7409394091f3bef76c24e Mon Sep 17 00:00:00 2001 From: Paulo Pinto Date: Wed, 11 Aug 2021 11:40:33 +0100 Subject: [PATCH] Refactor tests so that snapshots aren't used Signed-off-by: Paulo Pinto --- test/TextForEvent-test.ts | 85 +++++++++----- test/__snapshots__/TextForEvent-test.ts.snap | 113 ------------------- 2 files changed, 59 insertions(+), 139 deletions(-) delete mode 100644 test/__snapshots__/TextForEvent-test.ts.snap diff --git a/test/TextForEvent-test.ts b/test/TextForEvent-test.ts index b50996c186..ac9c8eba4d 100644 --- a/test/TextForEvent-test.ts +++ b/test/TextForEvent-test.ts @@ -23,64 +23,97 @@ function mockPinnedEvent( }); } +// Helper function that renders a component to a plain text string. +// Once snapshots are introduced in tests, this function will no longer be necessary, +// and should be replaced with snapshots. +function renderComponent(component): string { + const serializeObject = (object): string => { + if (typeof object === 'string') { + return object === ' ' ? '' : object; + } + + if (Array.isArray(object) && object.length === 1 && typeof object[0] === 'string') { + return object[0]; + } + + if (object['type'] !== undefined && typeof object['children'] !== undefined) { + return serializeObject(object.children); + } + + if (!Array.isArray(object)) { + return ''; + } + + return object.map(child => { + return serializeObject(child); + }).join(''); + }; + + return serializeObject(component.toJSON()); +} + describe('TextForEvent', () => { describe("TextForPinnedEvent", () => { SettingsStore.setValue("feature_pinning", null, SettingLevel.DEVICE, true); it("mentions message when a single message was pinned, with no previously pinned messages", () => { const event = mockPinnedEvent(['message-1']); - expect(textForEvent(event)).toBe( - "@foo:example.com pinned a message to this room. See all pinned messages.", - ); - + const plainText = textForEvent(event); const component = renderer.create(textForEvent(event, true)); - expect(component.toJSON()).toMatchSnapshot(); + + const expectedText = "@foo:example.com pinned a message to this room. See all pinned messages."; + expect(plainText).toBe(expectedText); + expect(renderComponent(component)).toBe(expectedText); }); it("mentions message when a single message was pinned, with multiple previously pinned messages", () => { const event = mockPinnedEvent(['message-3'], ['message-1', 'message-2']); - expect(textForEvent(event)).toBe( - "@foo:example.com pinned a message to this room. See all pinned messages.", - ); - + const plainText = textForEvent(event); const component = renderer.create(textForEvent(event, true)); - expect(component.toJSON()).toMatchSnapshot(); + + const expectedText = "@foo:example.com pinned a message to this room. See all pinned messages."; + expect(plainText).toBe(expectedText); + expect(renderComponent(component)).toBe(expectedText); }); it("shows generic text when multiple messages were pinned", () => { const event = mockPinnedEvent(['message-1', 'message-2', 'message-3'], ['message-1']); - expect(textForEvent(event)).toBe("@foo:example.com changed the pinned messages for the room."); - + const plainText = textForEvent(event); const component = renderer.create(textForEvent(event, true)); - expect(component.toJSON()).toMatchSnapshot(); + + const expectedText = "@foo:example.com changed the pinned messages for the room."; + expect(plainText).toBe(expectedText); + expect(renderComponent(component)).toBe(expectedText); }); it("mentions message when a single message was unpinned, with a single message previously pinned", () => { const event = mockPinnedEvent([], ['message-1']); - expect(textForEvent(event)).toBe( - "@foo:example.com unpinned a message from this room. See all pinned messages.", - ); - + const plainText = textForEvent(event); const component = renderer.create(textForEvent(event, true)); - expect(component.toJSON()).toMatchSnapshot(); + + const expectedText = "@foo:example.com unpinned a message from this room. See all pinned messages."; + expect(plainText).toBe(expectedText); + expect(renderComponent(component)).toBe(expectedText); }); it("mentions message when a single message was unpinned, with multiple previously pinned messages", () => { const event = mockPinnedEvent(['message-2'], ['message-1', 'message-2']); - expect(textForEvent(event)).toBe( - "@foo:example.com unpinned a message from this room. See all pinned messages.", - ); - + const plainText = textForEvent(event); const component = renderer.create(textForEvent(event, true)); - expect(component.toJSON()).toMatchSnapshot(); + + const expectedText = "@foo:example.com unpinned a message from this room. See all pinned messages."; + expect(plainText).toBe(expectedText); + expect(renderComponent(component)).toBe(expectedText); }); it("shows generic text when multiple messages were unpinned", () => { const event = mockPinnedEvent(['message-3'], ['message-1', 'message-2', 'message-3']); - expect(textForEvent(event)).toBe("@foo:example.com changed the pinned messages for the room."); - + const plainText = textForEvent(event); const component = renderer.create(textForEvent(event, true)); - expect(component.toJSON()).toMatchSnapshot(); + + const expectedText = "@foo:example.com changed the pinned messages for the room."; + expect(plainText).toBe(expectedText); + expect(renderComponent(component)).toBe(expectedText); }); }); }); diff --git a/test/__snapshots__/TextForEvent-test.ts.snap b/test/__snapshots__/TextForEvent-test.ts.snap deleted file mode 100644 index 2b73dbeb3d..0000000000 --- a/test/__snapshots__/TextForEvent-test.ts.snap +++ /dev/null @@ -1,113 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`TextForEvent TextForPinnedEvent mentions message when a single message was pinned, with multiple previously pinned messages 1`] = ` - - - @foo:example.com pinned - - a message - - to this room. See all - - pinned messages - - . - - -`; - -exports[`TextForEvent TextForPinnedEvent mentions message when a single message was pinned, with no previously pinned messages 1`] = ` - - - @foo:example.com pinned - - a message - - to this room. See all - - pinned messages - - . - - -`; - -exports[`TextForEvent TextForPinnedEvent mentions message when a single message was unpinned, with a single message previously pinned 1`] = ` - - - @foo:example.com unpinned - - a message - - from this room. See all - - pinned messages - - . - - -`; - -exports[`TextForEvent TextForPinnedEvent mentions message when a single message was unpinned, with multiple previously pinned messages 1`] = ` - - - @foo:example.com unpinned - - a message - - from this room. See all - - pinned messages - - . - - -`; - -exports[`TextForEvent TextForPinnedEvent shows generic text when multiple messages were pinned 1`] = ` - - - @foo:example.com changed the - - - pinned messages - - - for the room. - - -`; - -exports[`TextForEvent TextForPinnedEvent shows generic text when multiple messages were unpinned 1`] = ` - - - @foo:example.com changed the - - - pinned messages - - - for the room. - - -`;