update tests

This commit is contained in:
Sharon Kennedy 2020-09-06 15:24:41 -04:00
parent 91bec23c48
commit dbbe188adc
3 changed files with 30 additions and 20 deletions

View File

@ -24,7 +24,13 @@ export const mockInitCrypto = jest.fn()
export const mockStartClient = jest.fn(() => { export const mockStartClient = jest.fn(() => {
return Promise.resolve('value'); return Promise.resolve('value');
}); });
export const mockOnce = jest.fn() export const mockOnce = jest
.fn()
.mockImplementation((event, callback) => {
if (event === 'sync') {
callback('PREPARED')
}
})
export const mockStopClient = jest.fn(() => { export const mockStopClient = jest.fn(() => {
return Promise.resolve('value'); return Promise.resolve('value');
}); });

View File

@ -170,7 +170,9 @@ class ChatBox extends React.Component {
window.clearInterval(this.state.waitIntervalId) // no more waiting messages window.clearInterval(this.state.waitIntervalId) // no more waiting messages
} }
this.state.localStorage.clear() if (this.state.localStorage) {
this.state.localStorage.clear()
}
if (resetState) { if (resetState) {
this.setState(this.initialState) this.setState(this.initialState)

View File

@ -115,9 +115,7 @@ describe('Chatbox', () => {
expect(createClient).toHaveBeenCalled() expect(createClient).toHaveBeenCalled()
expect(mockInitCrypto).toHaveBeenCalled() expect(mockInitCrypto).toHaveBeenCalled()
expect(mockStartClient).toHaveBeenCalled() expect(mockStartClient).toHaveBeenCalled()
expect(mockCreateRoom).toHaveBeenCalled() expect(mockOnce).toHaveBeenCalled()
expect(mockSetPowerLevel).toHaveBeenCalled()
expect(mockOn).toHaveBeenCalled()
}) })
test('rejecting terms should not start chat', async () => { test('rejecting terms should not start chat', async () => {
@ -146,6 +144,10 @@ describe('Chatbox', () => {
acceptButton.simulate('click') acceptButton.simulate('click')
await waitForExpect(() => {
expect(mockOnce).toHaveBeenCalled()
});
await waitForExpect(() => { await waitForExpect(() => {
expect(mockCreateRoom).toHaveBeenCalled() expect(mockCreateRoom).toHaveBeenCalled()
}); });
@ -169,7 +171,7 @@ describe('Chatbox', () => {
}) })
test('decryption failure should lead to a new unencrypted chat', async () => { test('decryption failure should handle the message event and save the event ID in state', async () => {
const chatbox = mount(<Chatbox {...testConfig} />) const chatbox = mount(<Chatbox {...testConfig} />)
const dock = chatbox.find('button.dock') const dock = chatbox.find('button.dock')
const instance = chatbox.instance() const instance = chatbox.instance()
@ -183,25 +185,25 @@ describe('Chatbox', () => {
acceptButton.simulate('click') acceptButton.simulate('click')
await waitForExpect(() => { await waitForExpect(() => {
expect(mockCreateRoom).toHaveBeenCalled() expect(mockOnce).toHaveBeenCalled()
}); });
jest.spyOn(instance, 'initializeUnencryptedChat') jest.spyOn(instance, 'handleMessageEvent')
instance.handleDecryptionError()
instance.handleDecryptionError({
getId: () => 'test_event_id',
getType: () => 'm.message',
getSender: () => 'sender',
getRoomId: () => 'room id',
getContent: () => ({ body: 'test msg' }),
getTs: () => '123',
})
await waitForExpect(() => { await waitForExpect(() => {
expect(mockLeave).toHaveBeenCalled() expect(instance.handleMessageEvent).toHaveBeenCalled()
}); })
await waitForExpect(() => { expect(chatbox.state().decryptionErrors).toEqual({ 'test_event_id': true })
expect(mockStopClient).toHaveBeenCalled()
});
await waitForExpect(() => {
expect(mockClearStores).toHaveBeenCalled()
});
expect(instance.initializeUnencryptedChat).toHaveBeenCalled()
}) })
test('creating an unencrypted chat', async () => { test('creating an unencrypted chat', async () => {