Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/5-entities/transaction/thunks.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import type { TTagId } from '6-shared/types'
import { describe, expect, test } from 'vitest'
import { store } from 'store'
import { applyClientPatch } from 'store/data'
import { makeTransaction } from './makeTransaction'
import { getTransactionsById } from './model'
import { bulkEditTransactions } from './thunks'

function makeTr(id: string, tag: TTagId[] | null) {
return makeTransaction({
id,
user: 0,
date: '2024-01-01',
incomeInstrument: 2,
incomeAccount: 'acc',
outcomeInstrument: 2,
outcomeAccount: 'acc',
outcome: 100,
tag,
})
}

describe('bulkEditTransactions', () => {
test('adds a tag keeping own tags of every transaction', () => {
store.dispatch(
applyClientPatch({
transaction: [makeTr('tr1', ['tag1']), makeTr('tr2', ['tag2'])],
})
)
store.dispatch(
bulkEditTransactions(['tr1', 'tr2'], { tags: ['mixed', 'tag3'] })
)
const transactions = getTransactionsById(store.getState())
expect(transactions.tr1.tag).toEqual(['tag1', 'tag3'])
expect(transactions.tr2.tag).toEqual(['tag2', 'tag3'])
})

test('never writes the "mixed" placeholder to a transaction without tags', () => {
store.dispatch(
applyClientPatch({
transaction: [makeTr('tr3', ['tag1']), makeTr('tr4', null)],
})
)
store.dispatch(
bulkEditTransactions(['tr3', 'tr4'], { tags: ['mixed', 'tag3'] })
)
const transactions = getTransactionsById(store.getState())
expect(transactions.tr3.tag).toEqual(['tag1', 'tag3'])
expect(transactions.tr4.tag).toEqual(['tag3'])
})
})
3 changes: 2 additions & 1 deletion src/5-entities/transaction/thunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ const modifyTags = (prevTags: string[] | null, newTags?: string[]) => {
const addId = (id: string) =>
result.includes(id) || id === 'null' ? '' : result.push(id)
newTags?.forEach(id => {
if (id === 'mixed' && prevTags) prevTags.forEach(addId)
// 'mixed' is a placeholder for the transaction's own tags, never a real id
if (id === 'mixed') prevTags?.forEach(addId)
else addId(id)
})
return result
Expand Down