UNPKG

@aptpod/data-viz-create-visual-parts-react

Version:

template of npm project with typescript

199 lines (181 loc) 4.55 kB
import { shouldContentFit, buildColorWithOpacity, arrayIncludes, arraySearchIndexLe, arraySearchIndexGe, } from './utils' /** * shouldContentFit のテスト */ describe('shouldContentFit', () => { type TestData = { args: { colSpan: number rowSpan: number } exp: boolean } const testDatas: TestData[] = [ { args: { colSpan: 0, rowSpan: 0 }, exp: true, }, { args: { colSpan: 1, rowSpan: 0 }, exp: true, }, { args: { colSpan: 0, rowSpan: 1 }, exp: true, }, { args: { colSpan: 2, rowSpan: 1 }, exp: true, }, { args: { colSpan: 1, rowSpan: 2 }, exp: true, }, { args: { colSpan: 2, rowSpan: 2 }, exp: false, }, ] testDatas.forEach(({ args: { colSpan, rowSpan }, exp }) => { test(`returns ${exp} when colSpan=${colSpan}, rowSpan=${rowSpan}`, () => { expect(shouldContentFit(colSpan, rowSpan)).toBe(exp) }) }) }) /** * buildColorWithOpacity のテスト */ describe('buildColorWithOpacity', () => { type TestData = { args: { color: string opacity: number } exp: ReturnType<typeof buildColorWithOpacity> } const testDatas: TestData[] = [ { args: { color: 'invalid_color', opacity: 0, }, exp: null, }, { args: { color: 'white', opacity: 0.4, }, exp: 'rgba(255, 255, 255, 0.4)', }, { args: { color: '#00f', opacity: 1, }, exp: 'rgb(0, 0, 255)', }, { args: { color: '#f00', opacity: -1, }, exp: 'rgba(255, 0, 0, 0)', }, { args: { color: '#0f0', opacity: 999, }, exp: 'rgb(0, 255, 0)', }, ] testDatas.forEach(({ args: { color, opacity }, exp }) => { test(`returns ${exp} when color=${color}, opacity=${opacity}`, () => { expect(buildColorWithOpacity(color, opacity)).toBe(exp) }) }) }) /** * arrayIncludes のテスト */ describe('arrayIncludes', () => { type TestData = { args: { array: any[] searchElement: any } exp: ReturnType<typeof arrayIncludes> } const testDatas: TestData[] = [ { args: { array: [], searchElement: 0 }, exp: false }, { args: { array: [1, 0], searchElement: 0 }, exp: true }, { args: { array: ['a', 'b'], searchElement: 'c' }, exp: false }, ] testDatas.forEach(({ args: { array, searchElement }, exp }) => { const arrayString = JSON.stringify(array) test(`returns ${exp} when array=${arrayString}, searchElement=${searchElement}`, () => { expect(arrayIncludes(array, searchElement)).toBe(exp) }) }) }) /** * arraySearchIndexLe のテスト */ describe('arraySearchIndexLe', () => { type TestData = { args: { array: number[] searchElement: number } exp: number } const nums = [0, 1, 2, 3, 4, 5] const compiler = (a: number, b: number) => a - b const testDatas: TestData[] = [ { args: { array: nums, searchElement: -1 }, exp: -1 }, { args: { array: nums, searchElement: 0.5 }, exp: 0 }, { args: { array: nums, searchElement: 2 }, exp: 2 }, { args: { array: nums, searchElement: 4.5 }, exp: 4 }, { args: { array: nums, searchElement: 10 }, exp: 5 }, ] testDatas.forEach(({ args: { array, searchElement }, exp }) => { const arrayString = JSON.stringify(array) test(`returns ${exp} when array=${arrayString}, searchElement=${searchElement}`, () => { expect(arraySearchIndexLe(array, searchElement, compiler)).toBe(exp) }) }) }) /** * arraySearchIndexGe のテスト */ describe('arraySearchIndexGe', () => { type TestData = { args: { array: number[] searchElement: number } exp: number } const nums = [0, 1, 2, 3, 4, 5] const compiler = (a: number, b: number) => a - b const testDatas: TestData[] = [ { args: { array: nums, searchElement: -1 }, exp: 0 }, { args: { array: nums, searchElement: 0.5 }, exp: 1 }, { args: { array: nums, searchElement: 2 }, exp: 2 }, { args: { array: nums, searchElement: 4.5 }, exp: 5 }, { args: { array: nums, searchElement: 10 }, exp: -1 }, ] testDatas.forEach(({ args: { array, searchElement }, exp }) => { const arrayString = JSON.stringify(array) test(`returns ${exp} when array=${arrayString}, searchElement=${searchElement}`, () => { expect(arraySearchIndexGe(array, searchElement, compiler)).toBe(exp) }) }) })