main

mattermost/focalboard

Last updated at: 29/12/2023 09:48

octoUtils.test.ts

TLDR

This file contains test cases for the duplicateBlockTree and filterConditionValidOrDefault methods in the OctoUtils class. It also includes a helper function called createCardTree.

Methods

duplicateBlockTree

This method duplicates a block tree and returns the duplicated blocks along with the duplicated source block and an ID map. It ensures that the length of the new blocks is equal to the length of the original blocks. It also checks that the ID of the new source block is different from the ID of the original source block, and that the type of the new source block is the same as the original source block. Additionally, the boardId of the new source block and all new blocks should be the same as the original source block. Finally, for any text blocks in the new blocks, their parentId should be the ID of the new source block.

filterConditionValidOrDefault

This method returns the valid condition for the given field and condition type. It handles different field types, such as 'options', 'boolean', 'text', and 'date', and maps the condition types to their corresponding valid conditions.

END

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import {Block} from './blocks/block'
import {OctoUtils} from './octoUtils'

import {TestBlockFactory} from './test/testBlockFactory'

test('duplicateBlockTree: Card', async () => {
    const [blocks, sourceBlock] = createCardTree()

    const [newBlocks, newSourceBlock, idMap] = OctoUtils.duplicateBlockTree(blocks, sourceBlock.id)

    expect(newBlocks.length).toBe(blocks.length)
    expect(newSourceBlock.id).not.toBe(sourceBlock.id)
    expect(newSourceBlock.type).toBe(sourceBlock.type)

    // When duplicating a non-root block, the boardId should not be re-mapped
    expect(newSourceBlock.boardId).toBe(sourceBlock.boardId)
    expect(idMap[sourceBlock.id]).toBe(newSourceBlock.id)

    for (const newBlock of newBlocks) {
        expect(newBlock.boardId).toBe(newSourceBlock.boardId)
    }

    for (const textBlock of newBlocks.filter((o) => o.type === 'text')) {
        expect(textBlock.parentId).toBe(newSourceBlock.id)
    }
})

test('filterConditionValidOrDefault', async () => {
    // Test 'options'
    expect(OctoUtils.filterConditionValidOrDefault('options', 'includes')).toBe('includes')
    expect(OctoUtils.filterConditionValidOrDefault('options', 'notIncludes')).toBe('notIncludes')
    expect(OctoUtils.filterConditionValidOrDefault('options', 'isEmpty')).toBe('isEmpty')
    expect(OctoUtils.filterConditionValidOrDefault('options', 'isNotEmpty')).toBe('isNotEmpty')
    expect(OctoUtils.filterConditionValidOrDefault('options', 'is')).toBe('includes')

    expect(OctoUtils.filterConditionValidOrDefault('boolean', 'isSet')).toBe('isSet')
    expect(OctoUtils.filterConditionValidOrDefault('boolean', 'isNotSet')).toBe('isNotSet')
    expect(OctoUtils.filterConditionValidOrDefault('boolean', 'includes')).toBe('isSet')

    expect(OctoUtils.filterConditionValidOrDefault('text', 'is')).toBe('is')
    expect(OctoUtils.filterConditionValidOrDefault('text', 'contains')).toBe('contains')
    expect(OctoUtils.filterConditionValidOrDefault('text', 'notContains')).toBe('notContains')
    expect(OctoUtils.filterConditionValidOrDefault('text', 'startsWith')).toBe('startsWith')
    expect(OctoUtils.filterConditionValidOrDefault('text', 'notStartsWith')).toBe('notStartsWith')
    expect(OctoUtils.filterConditionValidOrDefault('text', 'endsWith')).toBe('endsWith')
    expect(OctoUtils.filterConditionValidOrDefault('text', 'notEndsWith')).toBe('notEndsWith')
    expect(OctoUtils.filterConditionValidOrDefault('text', 'isEmpty')).toBe('is')

    expect(OctoUtils.filterConditionValidOrDefault('date', 'is')).toBe('is')
    expect(OctoUtils.filterConditionValidOrDefault('date', 'isBefore')).toBe('isBefore')
    expect(OctoUtils.filterConditionValidOrDefault('date', 'isAfter')).toBe('isAfter')
    expect(OctoUtils.filterConditionValidOrDefault('date', 'isSet')).toBe('isSet')
    expect(OctoUtils.filterConditionValidOrDefault('date', 'isNotSet')).toBe('isNotSet')
    expect(OctoUtils.filterConditionValidOrDefault('date', 'isEmpty')).toBe('is')
})

function createCardTree(): [Block[], Block] {
    const blocks: Block[] = []

    const card = TestBlockFactory.createCard()
    card.id = 'card1'
    card.boardId = 'board1'
    blocks.push(card)

    for (let i = 0; i < 5; i++) {
        const textBlock = TestBlockFactory.createText(card)
        textBlock.id = `text${i}`
        blocks.push(textBlock)
    }

    return [blocks, card]
}