main

mattermost/focalboard

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

block.test.ts

TLDR

This file contains unit tests for the block module. It verifies that the createPatchesFromBlocks function correctly generates patches based on two blocks. The tests also check the behavior of adding and removing fields, as well as updating properties of the main object and reverting them back.

Methods

createPatchesFromBlocks

This method takes two blocks as input and generates patches based on their differences. It is used to compare and update blocks in the block module.

Classes

None

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {TestBlockFactory} from '../test/testBlockFactory'

import {createPatchesFromBlocks, createBlock} from './block'

describe('block tests', () => {
    const board = TestBlockFactory.createBoard()
    const card = TestBlockFactory.createCard(board)

    describe('correctly generate patches from two blocks', () => {
        it('should generate two empty patches for the same block', () => {
            const textBlock = TestBlockFactory.createText(card)
            const result = createPatchesFromBlocks(textBlock, textBlock)
            expect(result).toMatchSnapshot()
        })

        it('should add fields on the new fields added and remove it in the undo', () => {
            const oldBlock = TestBlockFactory.createText(card)
            const newBlock = createBlock(oldBlock)
            newBlock.fields.newField = 'new field'
            const result = createPatchesFromBlocks(newBlock, oldBlock)
            expect(result).toMatchSnapshot()
        })

        it('should remove field on the new block added and add it again in the undo', () => {
            const oldBlock = TestBlockFactory.createText(card)
            const newBlock = createBlock(oldBlock)
            oldBlock.fields.test = 'test'
            const result = createPatchesFromBlocks(newBlock, oldBlock)
            expect(result).toMatchSnapshot()
        })

        it('should update propertie on the main object and revert it back on the undo', () => {
            const oldBlock = TestBlockFactory.createText(card)
            const newBlock = createBlock(oldBlock)
            oldBlock.parentId = 'old-parent-id'
            newBlock.parentId = 'new-parent-id'
            const result = createPatchesFromBlocks(newBlock, oldBlock)
            expect(result).toMatchSnapshot()
        })
    })
})