Clean up <LeftPane /> tests

This commit is contained in:
Evan Hahn 2020-10-19 14:16:27 -05:00 committed by Evan Hahn
parent 7d29cb5edf
commit 544995cc21

View file

@ -1,122 +1,34 @@
import { expect } from 'chai'; import React from 'react';
import { assert } from 'chai';
import { import { LeftPane, RowType, HeaderType } from '../../components/LeftPane';
LeftPane,
RowType,
PropsType,
HeaderType,
} from '../../components/LeftPane';
import { setup as setupI18n } from '../../../js/modules/i18n'; import { setup as setupI18n } from '../../../js/modules/i18n';
import enMessages from '../../../_locales/en/messages.json'; import enMessages from '../../../_locales/en/messages.json';
const i18n = setupI18n('en', enMessages); const i18n = setupI18n('en', enMessages);
describe('LeftPane', () => { describe('LeftPane', () => {
/* eslint-disable @typescript-eslint/no-explicit-any */
const defaultProps = { const defaultProps = {
archivedConversations: [], archivedConversations: [],
conversations: [], conversations: [],
i18n, i18n,
openConversationInternal: () => null, openConversationInternal: () => null,
pinnedConversations: [], pinnedConversations: [],
renderExpiredBuildDialog: () => '<div />' as any, renderExpiredBuildDialog: () => <div />,
renderMainHeader: () => '<div />' as any, renderMainHeader: () => <div />,
renderMessageSearchResult: () => '<div />' as any, renderMessageSearchResult: () => <div />,
renderNetworkStatus: () => '<div />' as any, renderNetworkStatus: () => <div />,
renderRelinkDialog: () => '<div />' as any, renderRelinkDialog: () => <div />,
renderUpdateDialog: () => '<div />' as any, renderUpdateDialog: () => <div />,
showArchivedConversations: () => null, showArchivedConversations: () => null,
showInbox: () => null, showInbox: () => null,
startNewConversation: () => null, startNewConversation: () => null,
}; };
/* eslint-enable @typescript-eslint/no-explicit-any */
describe('getRowFromIndex', () => { describe('getRowFromIndex', () => {
let leftPane: LeftPane;
describe('with pinned, non-pinned, and archived chats', () => {
it('returns headers, conversations, and an archived button', () => {
leftPane = new LeftPane({
...defaultProps,
pinnedConversations: [
{
id: 'philly-convo',
isPinned: true,
isSelected: false,
lastUpdated: Date.now(),
title: 'Philip Glass',
type: 'direct',
},
{
id: 'robbo-convo',
isPinned: true,
isSelected: false,
lastUpdated: Date.now(),
title: 'Robert Moog',
type: 'direct',
},
],
conversations: [
{
id: 'etta-convo',
isSelected: false,
lastUpdated: Date.now(),
title: 'Etta James',
type: 'direct',
},
{
id: 'kimbra-convo',
isPinned: false,
isSelected: false,
lastUpdated: Date.now(),
title: 'Kimbra',
type: 'direct',
},
],
archivedConversations: [
{
id: 'jerry-convo',
isSelected: false,
lastUpdated: Date.now(),
title: 'Jerry Jordan',
type: 'direct',
},
],
});
expect(leftPane.getRowFromIndex(0)).to.eql({
headerType: HeaderType.Pinned,
type: RowType.Header,
});
expect(leftPane.getRowFromIndex(1)).to.eql({
index: 0,
type: RowType.PinnedConversation,
});
expect(leftPane.getRowFromIndex(2)).to.eql({
index: 1,
type: RowType.PinnedConversation,
});
expect(leftPane.getRowFromIndex(3)).to.eql({
headerType: HeaderType.Chats,
type: RowType.Header,
});
expect(leftPane.getRowFromIndex(4)).to.eql({
index: 0,
type: RowType.Conversation,
});
expect(leftPane.getRowFromIndex(5)).to.eql({
index: 1,
type: RowType.Conversation,
});
expect(leftPane.getRowFromIndex(6)).to.eql({
type: RowType.ArchiveButton,
});
});
});
describe('given only pinned chats', () => { describe('given only pinned chats', () => {
beforeEach(function beforeEach() { it('returns pinned chats, not headers', () => {
const props: PropsType = { const leftPane = new LeftPane({
...defaultProps, ...defaultProps,
pinnedConversations: [ pinnedConversations: [
{ {
@ -136,17 +48,13 @@ describe('LeftPane', () => {
type: 'direct', type: 'direct',
}, },
], ],
};
leftPane = new LeftPane(props);
}); });
it('return pinned chats, not headers', () => { assert.deepEqual(leftPane.getRowFromIndex(0), {
expect(leftPane.getRowFromIndex(0)).to.eql({
index: 0, index: 0,
type: RowType.PinnedConversation, type: RowType.PinnedConversation,
}); });
assert.deepEqual(leftPane.getRowFromIndex(1), {
expect(leftPane.getRowFromIndex(1)).to.eql({
index: 1, index: 1,
type: RowType.PinnedConversation, type: RowType.PinnedConversation,
}); });
@ -155,7 +63,7 @@ describe('LeftPane', () => {
describe('given only non-pinned chats', () => { describe('given only non-pinned chats', () => {
it('returns conversations, not headers', () => { it('returns conversations, not headers', () => {
const props: PropsType = { const leftPane = new LeftPane({
...defaultProps, ...defaultProps,
conversations: [ conversations: [
{ {
@ -174,15 +82,13 @@ describe('LeftPane', () => {
type: 'direct', type: 'direct',
}, },
], ],
}; });
leftPane = new LeftPane(props);
expect(leftPane.getRowFromIndex(0)).to.eql({ assert.deepEqual(leftPane.getRowFromIndex(0), {
index: 0, index: 0,
type: RowType.Conversation, type: RowType.Conversation,
}); });
assert.deepEqual(leftPane.getRowFromIndex(1), {
expect(leftPane.getRowFromIndex(1)).to.eql({
index: 1, index: 1,
type: RowType.Conversation, type: RowType.Conversation,
}); });
@ -190,8 +96,8 @@ describe('LeftPane', () => {
}); });
describe('given only pinned and non-pinned chats', () => { describe('given only pinned and non-pinned chats', () => {
beforeEach(function beforeEach() { it('returns headers and conversations', () => {
const props: PropsType = { const leftPane = new LeftPane({
...defaultProps, ...defaultProps,
conversations: [ conversations: [
{ {
@ -212,94 +118,30 @@ describe('LeftPane', () => {
type: 'direct', type: 'direct',
}, },
], ],
};
leftPane = new LeftPane(props);
}); });
it('returns headers and conversations', () => { assert.deepEqual(leftPane.getRowFromIndex(0), {
expect(leftPane.getRowFromIndex(0)).to.eql({
headerType: HeaderType.Pinned, headerType: HeaderType.Pinned,
type: RowType.Header, type: RowType.Header,
}); });
assert.deepEqual(leftPane.getRowFromIndex(1), {
expect(leftPane.getRowFromIndex(1)).to.eql({
index: 0, index: 0,
type: RowType.PinnedConversation, type: RowType.PinnedConversation,
}); });
assert.deepEqual(leftPane.getRowFromIndex(2), {
expect(leftPane.getRowFromIndex(2)).to.eql({
headerType: HeaderType.Chats, headerType: HeaderType.Chats,
type: RowType.Header, type: RowType.Header,
}); });
assert.deepEqual(leftPane.getRowFromIndex(3), {
expect(leftPane.getRowFromIndex(3)).to.eql({
index: 0, index: 0,
type: RowType.Conversation, type: RowType.Conversation,
}); });
}); });
}); });
describe('given only pinned and archived chats', () => {
it('shows the pinned chats with no headers', () => {
leftPane = new LeftPane({
...defaultProps,
pinnedConversations: [
{
id: 'philly-convo',
isPinned: true,
isSelected: false,
lastUpdated: Date.now(),
title: 'Philip Glass',
type: 'direct',
},
],
archivedConversations: [
{
id: 'fred-convo',
isSelected: false,
lastUpdated: Date.now(),
title: 'Fred Willard',
type: 'direct',
},
],
});
expect(leftPane.getRowFromIndex(0)).to.eql({
index: 0,
type: RowType.PinnedConversation,
});
expect(leftPane.getRowFromIndex(1)).to.eql({
type: RowType.ArchiveButton,
});
});
});
describe("given only archived conversations, which we're not showing", () => {
it('returns a single row, the archive button', () => {
leftPane = new LeftPane({
...defaultProps,
archivedConversations: [
{
id: 'jerry-convo',
isSelected: false,
lastUpdated: Date.now(),
title: 'Jerry Jordan',
type: 'direct',
},
],
showArchived: false,
});
expect(leftPane.getRowFromIndex(0)).to.eql({
type: RowType.ArchiveButton,
});
});
});
describe('given not showing archive with archived conversation', () => { describe('given not showing archive with archived conversation', () => {
beforeEach(function beforeEach() { it('returns an archive button last', () => {
const props: PropsType = { const leftPane = new LeftPane({
...defaultProps, ...defaultProps,
archivedConversations: [ archivedConversations: [
{ {
@ -320,21 +162,17 @@ describe('LeftPane', () => {
}, },
], ],
showArchived: false, showArchived: false,
};
leftPane = new LeftPane(props);
}); });
it('returns an archive button last', () => { assert.deepEqual(leftPane.getRowFromIndex(1), {
expect(leftPane.getRowFromIndex(1)).to.eql({
type: RowType.ArchiveButton, type: RowType.ArchiveButton,
}); });
}); });
}); });
describe('given showing archive and archive chats', () => { describe('given showing archive and archive chats', () => {
beforeEach(function beforeEach() { it('returns archived conversations', () => {
const props: PropsType = { const leftPane = new LeftPane({
...defaultProps, ...defaultProps,
archivedConversations: [ archivedConversations: [
{ {
@ -346,13 +184,9 @@ describe('LeftPane', () => {
}, },
], ],
showArchived: true, showArchived: true,
};
leftPane = new LeftPane(props);
}); });
it('returns archived conversations', () => { assert.deepEqual(leftPane.getRowFromIndex(0), {
expect(leftPane.getRowFromIndex(0)).to.eql({
index: 0, index: 0,
type: RowType.ArchivedConversation, type: RowType.ArchivedConversation,
}); });