Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jest): improve numerous store tests #4641

Merged
merged 9 commits into from
Sep 8, 2022
31 changes: 31 additions & 0 deletions store/chat/__snapshots__/mutations.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,37 @@ Object {
}
`;

exports[`misc module.default.addFile with existing files array 1`] = `
Object {
"undefined": Array [
Object {
"file": "path2",
"nsfw": Object {
"checking": false,
"status": false,
},
"url": "string2",
},
Object {
"file": "path2",
"nsfw": Object {
"checking": false,
"status": false,
},
"url": "string2",
},
Object {
"file": "path3",
"nsfw": Object {
"checking": false,
"status": false,
},
"url": "string3",
},
],
}
`;

exports[`module.default.addFile 0 1`] = `undefined`;

exports[`module.default.addFile 1 1`] = `undefined`;
Expand Down
88 changes: 68 additions & 20 deletions store/chat/mutations.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ChatState, ChatFileUpload } from './types'
import { ChatFileUpload } from './types'
import InitialChatState from './state'
import * as module from '~/store/chat/mutations'

describe('module.default.setChatReply', () => {
Expand Down Expand Up @@ -233,6 +234,34 @@ describe('misc', () => {
module.default.addFile(state, obj)
expect(state.files).toMatchSnapshot()
})

test('module.default.addFile with existing files array', () => {
const obj = {
file: {
file: 'path2',
url: 'string2',
nsfw: {
checking: false,
status: false,
},
},
address: 'address1',
}
module.default.addFile(state, obj)
const obj2 = {
file: {
file: 'path3',
url: 'string3',
nsfw: {
checking: false,
status: false,
},
},
address: 'address1',
}
module.default.addFile(state, obj2)
expect(state.files).toMatchSnapshot()
})
})

describe('module.default.chatText', () => {
Expand Down Expand Up @@ -980,25 +1009,6 @@ describe.skip('module.default.deleteFiles', () => {
})

describe('misc', () => {
const InitialChatState = (): ChatState => ({
replies: [],
chatTexts: [],
files: {},
countError: false,
currentChat: {
direction: 'TOP',
hasNextPage: true,
isMessagesLoading: false,
isScrollOver: true,
lastLoadedMessageId: '',
messages: [],
offset: 0,
page: 1,
showOlderMessagesInfo: false,
size: 10,
},
})

test('module.setCountError', () => {
const argument = true
const localState = { ...InitialChatState }
Expand Down Expand Up @@ -1103,4 +1113,42 @@ describe('misc', () => {
],
})
})

test('module.default.deleteFiles', () => {
const state = {
...InitialChatState,
files: {
file1: [
{
file: 'path2',
url: 'string2',
nsfw: {
checking: false,
status: false,
},
progress: 0,
},
],
},
}
Comment on lines +1118 to +1133
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a good technique to keep things readable, keep it up!


module.default.deleteFiles(state, 'file1')
expect(state.files).toEqual({})
})

test('module.default.setDraftMessage', () => {
const state = {
...InitialChatState,
draftMessages: {
conversation_id: 'no_message',
},
}
const argument = {
conversationId: 'conversation_id',
message: 'message',
}

module.default.setDraftMessage(state, argument)
expect(state.draftMessages[argument.conversationId]).toBe(argument.message)
})
})
12 changes: 12 additions & 0 deletions store/conversation/mutations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ConversationConnection,
ConversationState,
} from './types'
import InitialConversationState from './state'
import * as mutations from '~/store/conversation/mutations'

describe('mutations.default.setConversation', () => {
Expand Down Expand Up @@ -264,4 +265,15 @@ describe('misc', () => {
// Both argument 2 and argument 3 are identical; both assertions will return true
expect(result).toBe(undefined)
})

test('mutations.default.resetConversation', () => {
const state = InitialConversationState()

mutations.default.resetConversation(state)

expect(state.id).toEqual('')
expect(state.type).toEqual('friend')
expect(state.calling).toEqual(false)
expect(state.participants).toEqual([])
})
})
44 changes: 44 additions & 0 deletions store/ui/actions.test.disabled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import module from './actions'

describe('Test store/ui', () => {
it('should set messages', () => {
const commit = jest.fn()
const argument = ['message 1', 'message 2']

module.setMessages({ commit }, argument)

expect(commit).toHaveBeenCalled()
expect(commit).toHaveBeenCalledWith(argument)
})

it('should set isScrollOver', () => {
const commit = jest.fn()
const status = true

module.setIsScrollOver({ commit }, status)

expect(commit).toHaveBeenCalledWith(status)
})

it('should set activeChannel', () => {
const commit = jest.fn()
const channel = {
type: '',
id: '123',
name: 'John Doe',
}

module.setActiveChannel({ commit }, channel)

expect(commit).toHaveBeenCalledWith(channel)
})

it('should set isReacted', () => {
const commit = jest.fn()
const argument = false

module.setIsReacted({ commit }, argument)

expect(commit).toHaveBeenCalledWith(argument)
})
})