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

Unit tests: Refactor FetchMock

This commit is contained in:
Chen-I Lim 2020-12-07 15:40:32 -08:00
parent 832509c766
commit 45afa34c72
6 changed files with 65 additions and 51 deletions

View File

@ -29,7 +29,8 @@
},
"collectCoverage": true,
"collectCoverageFrom": [
"src/**/*.{ts,tsx,js,jsx}"
"src/**/*.{ts,tsx,js,jsx}",
"!src/test/**"
]
},
"devDependencies": {

View File

@ -8,39 +8,35 @@ import {IBlock} from './blocks/block'
import {MutableBoard} from './blocks/board'
import octoClient from './octoClient'
import 'isomorphic-fetch'
import {FetchMock} from './test/fetchMock'
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('OctoClient: get blocks', async () => {
const blocks = createBoards()
fetchMock.mockReturnValueOnce(jsonResponse(JSON.stringify(blocks)))
FetchMock.fn.mockReturnValueOnce(FetchMock.jsonResponse(JSON.stringify(blocks)))
let boards = await octoClient.getBlocksWithType('board')
expect(boards.length).toBe(blocks.length)
fetchMock.mockReturnValueOnce(jsonResponse(JSON.stringify(blocks)))
FetchMock.fn.mockReturnValueOnce(FetchMock.jsonResponse(JSON.stringify(blocks)))
boards = await octoClient.getSubtree()
expect(boards.length).toBe(blocks.length)
fetchMock.mockReturnValueOnce(jsonResponse(JSON.stringify(blocks)))
FetchMock.fn.mockReturnValueOnce(FetchMock.jsonResponse(JSON.stringify(blocks)))
boards = await octoClient.exportFullArchive()
expect(boards.length).toBe(blocks.length)
fetchMock.mockReturnValueOnce(jsonResponse(JSON.stringify(blocks)))
FetchMock.fn.mockReturnValueOnce(FetchMock.jsonResponse(JSON.stringify(blocks)))
const parentId = 'id1'
boards = await octoClient.getBlocksWithParent(parentId)
expect(boards.length).toBe(blocks.length)
fetchMock.mockReturnValueOnce(jsonResponse(JSON.stringify(blocks)))
FetchMock.fn.mockReturnValueOnce(FetchMock.jsonResponse(JSON.stringify(blocks)))
boards = await octoClient.getBlocksWithParent(parentId, 'board')
expect(boards.length).toBe(blocks.length)
})
@ -50,8 +46,8 @@ test('OctoClient: insert blocks', async () => {
await octoClient.insertBlocks(blocks)
expect(fetchMock).toBeCalledTimes(1)
expect(fetchMock).toHaveBeenCalledWith(
expect(FetchMock.fn).toBeCalledTimes(1)
expect(FetchMock.fn).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
method: 'POST',
@ -64,8 +60,8 @@ test('OctoClient: importFullArchive', async () => {
await octoClient.importFullArchive(blocks)
expect(fetchMock).toBeCalledTimes(1)
expect(fetchMock).toHaveBeenCalledWith(
expect(FetchMock.fn).toBeCalledTimes(1)
expect(FetchMock.fn).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
method: 'POST',
@ -73,11 +69,6 @@ test('OctoClient: importFullArchive', async () => {
}))
})
async function jsonResponse(json: string) {
const response = new Response(json)
return response
}
function createBoards(): IBlock[] {
const blocks = []

View File

@ -0,0 +1,16 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
class FetchMock {
static fn = jest.fn(async () => {
const response = new Response()
return response
})
static async jsonResponse(json: string): Promise<Response> {
const response = new Response(json)
return response
}
}
export {FetchMock}

View File

@ -0,0 +1,24 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
// Disable console log
console.log = jest.fn()
import 'isomorphic-fetch'
import {TestBlockFactory} from '../test/block'
import {MutableCardTree} from './cardTree'
const fetchMock = jest.fn(async () => {
const response = new Response()
return response
})
global.fetch = fetchMock
beforeEach(() => {
fetchMock.mockReset()
})
test('CardTree', async () => {
})

View File

@ -6,18 +6,14 @@ console.log = jest.fn()
import 'isomorphic-fetch'
import {TestBlockFactory} from '../test/block'
import {FetchMock} from '../test/fetchMock'
import {MutableCardTree} from './cardTree'
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 () => {
@ -28,11 +24,11 @@ test('CardTree', async () => {
const image = TestBlockFactory.createImage(card)
const divider = TestBlockFactory.createDivider(card)
fetchMock.mockReturnValueOnce(jsonResponse(JSON.stringify([card, comment, text, image, divider])))
FetchMock.fn.mockReturnValueOnce(FetchMock.jsonResponse(JSON.stringify([card, comment, text, image, divider])))
const cardTree = new MutableCardTree(card.id)
await cardTree.sync()
expect(fetchMock).toBeCalledTimes(1)
expect(FetchMock.fn).toBeCalledTimes(1)
expect(cardTree.card).toEqual(card)
expect(cardTree.comments).toEqual([comment])
expect(cardTree.contents).toEqual([text, image, divider])
@ -57,8 +53,3 @@ test('CardTree', async () => {
expect(cardTree2).toEqual(cardTree)
expect(cardTree2.card).toEqual(cardTree.card)
})
async function jsonResponse(json: string) {
const response = new Response(json)
return response
}

View File

@ -4,18 +4,14 @@ console.log = jest.fn()
import 'isomorphic-fetch'
import {TestBlockFactory} from '../test/block'
import {FetchMock} from '../test/fetchMock'
import {MutableWorkspaceTree} from './workspaceTree'
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('WorkspaceTree', async () => {
@ -25,12 +21,12 @@ test('WorkspaceTree', async () => {
const view = TestBlockFactory.createBoardView()
// Sync
fetchMock.mockReturnValueOnce(jsonResponse(JSON.stringify([board, boardTemplate])))
fetchMock.mockReturnValueOnce(jsonResponse(JSON.stringify([view])))
FetchMock.fn.mockReturnValueOnce(FetchMock.jsonResponse(JSON.stringify([board, boardTemplate])))
FetchMock.fn.mockReturnValueOnce(FetchMock.jsonResponse(JSON.stringify([view])))
const workspaceTree = new MutableWorkspaceTree()
await workspaceTree.sync()
expect(fetchMock).toBeCalledTimes(2)
expect(FetchMock.fn).toBeCalledTimes(2)
expect(workspaceTree.boards).toEqual([board])
expect(workspaceTree.boardTemplates).toEqual([boardTemplate])
expect(workspaceTree.views).toEqual([view])
@ -60,8 +56,3 @@ test('WorkspaceTree', async () => {
expect(workspaceTree2.boardTemplates).toEqual(workspaceTree.boardTemplates)
expect(workspaceTree2.views).toEqual(workspaceTree.views)
})
async function jsonResponse(json: string) {
const response = new Response(json)
return response
}