1
0
mirror of https://github.com/mattermost/focalboard.git synced 2024-12-30 14:00:07 +02:00

Unit tests: BoardTree

This commit is contained in:
Chen-I Lim 2020-12-07 16:36:02 -08:00
parent 45afa34c72
commit 8f441747dd
5 changed files with 125 additions and 36 deletions

View File

@ -1,7 +1,7 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {TestBlockFactory} from '../test/block'
import {TestBlockFactory} from '../test/testBlockFactory'
import {MutableBoard} from './board'
import {MutableBoardView} from './boardView'

View File

@ -1,7 +1,7 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {IPropertyOption, IPropertyTemplate, MutableBoard} from '../blocks/board'
import {Board, IPropertyOption, IPropertyTemplate, MutableBoard} from '../blocks/board'
import {MutableBoardView} from '../blocks/boardView'
import {Card, MutableCard} from '../blocks/card'
import {MutableCommentBlock} from '../blocks/commentBlock'
@ -10,27 +10,25 @@ import {ImageBlock, MutableImageBlock} from '../blocks/imageBlock'
import {MutableTextBlock, TextBlock} from '../blocks/textBlock'
import {FilterClause} from '../filterClause'
import {FilterGroup} from '../filterGroup'
import {Utils} from '../utils'
class TestBlockFactory {
static createBoard(): MutableBoard {
const board = new MutableBoard()
board.parentId = 'parent'
board.rootId = 'root'
board.title = 'title'
board.rootId = board.id
board.title = 'board title'
board.description = 'description'
board.showDescription = true
board.icon = 'i'
for (let i = 0; i < 5; i++) {
for (let i = 0; i < 3; i++) {
const propertyOption: IPropertyOption = {
id: 'property1',
value: 'value1',
id: 'value1',
value: 'value 1',
color: 'color1',
}
const propertyTemplate: IPropertyTemplate = {
id: Utils.createGuid(),
name: 'Status',
id: `property${i + 1}`,
name: `Property ${i + 1}`,
type: 'select',
options: [propertyOption],
}
@ -40,14 +38,14 @@ class TestBlockFactory {
return board
}
static createBoardView(): MutableBoardView {
static createBoardView(board?: Board): MutableBoardView {
const view = new MutableBoardView()
view.parentId = 'parent'
view.rootId = 'root'
view.title = 'title'
view.parentId = board ? board.id : 'parent'
view.rootId = board ? board.rootId : 'root'
view.title = 'view title'
view.viewType = 'board'
view.groupById = 'groupId'
view.hiddenOptionIds = ['option1', 'option2', 'option3']
view.groupById = 'property1'
view.hiddenOptionIds = ['value1']
view.cardOrder = ['card1', 'card2', 'card3']
view.sortOptions = [
{
@ -76,10 +74,10 @@ class TestBlockFactory {
return view
}
static createCard(): MutableCard {
static createCard(board?: Board): MutableCard {
const card = new MutableCard()
card.parentId = 'parent'
card.rootId = 'root'
card.parentId = board ? board.id : 'parent'
card.rootId = board ? board.rootId : 'root'
card.title = 'title'
card.icon = 'i'
card.properties.property1 = 'value1'

View File

@ -3,22 +3,113 @@
// Disable console log
console.log = jest.fn()
console.error = jest.fn()
import 'isomorphic-fetch'
import {TestBlockFactory} from '../test/block'
import {TestBlockFactory} from '../test/testBlockFactory'
import {FetchMock} from '../test/fetchMock'
import {MutableCardTree} from './cardTree'
import {MutableBoardTree} from './boardTree'
const fetchMock = jest.fn(async () => {
const response = new Response()
return response
})
global.fetch = fetchMock
global.fetch = FetchMock.fn
beforeEach(() => {
fetchMock.mockReset()
FetchMock.fn.mockReset()
})
test('CardTree', async () => {
test('BoardTree', async () => {
const board = TestBlockFactory.createBoard()
const view = TestBlockFactory.createBoardView(board)
const view2 = TestBlockFactory.createBoardView(board)
view2.sortOptions = []
const card = TestBlockFactory.createCard(board)
const cardTemplate = TestBlockFactory.createCard(board)
cardTemplate.isTemplate = true
// Sync
FetchMock.fn.mockReturnValueOnce(FetchMock.jsonResponse(JSON.stringify([board, view, view2, card, cardTemplate])))
const boardTree = new MutableBoardTree(board.id)
await boardTree.sync()
expect(FetchMock.fn).toBeCalledTimes(1)
expect(boardTree.board).toEqual(board)
expect(boardTree.views).toEqual([view, view2])
expect(boardTree.allCards).toEqual([card])
expect(boardTree.cardTemplates).toEqual([cardTemplate])
expect(boardTree.allBlocks).toEqual([board, view, view2, card, cardTemplate])
// Group / filter with sort
boardTree.setActiveView(view.id)
expect(boardTree.activeView).toEqual(view)
expect(boardTree.cards).toEqual([card])
// Group / filter without sort
boardTree.setActiveView(view2.id)
expect(boardTree.activeView).toEqual(view2)
expect(boardTree.cards).toEqual([card])
// Invalid view, defaults to first view
boardTree.setActiveView('invalid id')
expect(boardTree.activeView).toEqual(view)
// Incremental update
const view3 = TestBlockFactory.createBoardView(board)
const card2 = TestBlockFactory.createCard(board)
const cardTemplate2 = TestBlockFactory.createCard(board)
cardTemplate2.isTemplate = true
expect(boardTree.incrementalUpdate([view3, card2, cardTemplate2])).toBe(true)
expect(boardTree.views).toEqual([view, view2, view3])
expect(boardTree.allCards).toEqual([card, card2])
expect(boardTree.cardTemplates).toEqual([cardTemplate, cardTemplate2])
// Group / filter with sort
boardTree.setActiveView(view.id)
expect(boardTree.activeView).toEqual(view)
expect(boardTree.cards).toEqual([card, card2])
// Group / filter without sort
boardTree.setActiveView(view2.id)
expect(boardTree.activeView).toEqual(view2)
expect(boardTree.cards).toEqual([card, card2])
// Incremental update: No change
const anotherBoard = TestBlockFactory.createBoard()
const card4 = TestBlockFactory.createCard(anotherBoard)
expect(boardTree.incrementalUpdate([anotherBoard, card4])).toBe(false)
// Copy
const boardTree2 = boardTree.mutableCopy()
expect(boardTree2.board).toEqual(boardTree.board)
expect(boardTree2.views).toEqual(boardTree.views)
expect(boardTree2.allCards).toEqual(boardTree.allCards)
expect(boardTree2.cardTemplates).toEqual(boardTree.cardTemplates)
expect(boardTree2.allBlocks).toEqual(boardTree.allBlocks)
// Search text
const searchText = 'search text'
expect(boardTree.getSearchText()).toBeUndefined()
boardTree.setSearchText(searchText)
expect(boardTree.getSearchText()).toBe(searchText)
})
test('BoardTree: defaults', async () => {
const board = TestBlockFactory.createBoard()
board.cardProperties = []
// Sync
FetchMock.fn.mockReturnValueOnce(FetchMock.jsonResponse(JSON.stringify([board])))
const boardTree = new MutableBoardTree(board.id)
await boardTree.sync()
expect(FetchMock.fn).toBeCalledTimes(1)
expect(boardTree.views.length).toEqual(1)
expect(boardTree.allCards).toEqual([])
expect(boardTree.cardTemplates).toEqual([])
// Match everything except for cardProperties
board.cardProperties = boardTree.board.cardProperties
expect(boardTree.board).toEqual(board)
})

View File

@ -5,7 +5,7 @@
console.log = jest.fn()
import 'isomorphic-fetch'
import {TestBlockFactory} from '../test/block'
import {TestBlockFactory} from '../test/testBlockFactory'
import {FetchMock} from '../test/fetchMock'
import {MutableCardTree} from './cardTree'
@ -50,6 +50,7 @@ test('CardTree', async () => {
// Copy
const cardTree2 = cardTree.mutableCopy()
expect(cardTree2).toEqual(cardTree)
expect(cardTree2.card).toEqual(cardTree.card)
expect(cardTree2.comments).toEqual(cardTree.comments)
expect(cardTree2.contents).toEqual(cardTree.contents)
})

View File

@ -3,7 +3,7 @@
console.log = jest.fn()
import 'isomorphic-fetch'
import {TestBlockFactory} from '../test/block'
import {TestBlockFactory} from '../test/testBlockFactory'
import {FetchMock} from '../test/fetchMock'
import {MutableWorkspaceTree} from './workspaceTree'
@ -18,7 +18,7 @@ test('WorkspaceTree', async () => {
const board = TestBlockFactory.createBoard()
const boardTemplate = TestBlockFactory.createBoard()
boardTemplate.isTemplate = true
const view = TestBlockFactory.createBoardView()
const view = TestBlockFactory.createBoardView(board)
// Sync
FetchMock.fn.mockReturnValueOnce(FetchMock.jsonResponse(JSON.stringify([board, boardTemplate])))
@ -35,7 +35,7 @@ test('WorkspaceTree', async () => {
const board2 = TestBlockFactory.createBoard()
const boardTemplate2 = TestBlockFactory.createBoard()
boardTemplate2.isTemplate = true
const view2 = TestBlockFactory.createBoardView()
const view2 = TestBlockFactory.createBoardView(board2)
expect(workspaceTree.incrementalUpdate([board2, boardTemplate2, view2])).toBe(true)
expect(workspaceTree.boards).toEqual([board, board2])
@ -51,7 +51,6 @@ test('WorkspaceTree', async () => {
// Copy
const workspaceTree2 = workspaceTree.mutableCopy()
expect(workspaceTree2).toEqual(workspaceTree)
expect(workspaceTree2.boards).toEqual(workspaceTree.boards)
expect(workspaceTree2.boardTemplates).toEqual(workspaceTree.boardTemplates)
expect(workspaceTree2.views).toEqual(workspaceTree.views)