import { join } from 'path' /** * The board list's type and state chips say how many boards each would show, in the list's scope (every board, the * caller's favorites, the caller's own), under the list's other conditions - the chip's own field left out. */ const { createSqliteTestDataSource } = require(join(__dirname, '..', '..', 'ops-master', 'test', 'support', 'database.cjs')) const DIST = join(__dirname, '..', 'dist-server') const { Board } = require(join(DIST, 'service', 'board', 'board.js')) const { boardCounts } = require(join(DIST, 'service', 'board', 'board-counts.js')) const { Favorite } = require('@things-factory/fav-base') const { User } = require('@things-factory/auth-base') const shell = require('@things-factory/shell') let db: any let here: any let me: any let other: any beforeAll(async () => { db = createSqliteTestDataSource([Board, Favorite, User]) await db.initialize() shell.addDataSource('default', db) here = await db.getRepository(shell.Domain).save({ name: 'counts', subdomain: 'counts' }) ;[me, other] = await db.getRepository(User).save([ { name: 'me', email: 'me@example.test' }, { name: 'other', email: 'other@example.test' } ]) const boards = await db.getRepository(Board).save([ { domain: here, name: 'line A', type: 'main', state: 'released', creator: me, model: '{}' }, { domain: here, name: 'line B', type: 'main', state: 'draft', creator: other, model: '{}' }, { domain: here, name: 'popup C', type: 'popup', state: 'draft', creator: me, model: '{}' } ]) await db.getRepository(Favorite).save({ domain: here, user: me, routing: boards[1].id }) }) afterAll(async () => { await db?.destroy() }) const counts = async (field: string, scope: string | null, params: any = {}) => Object.fromEntries((await boardCounts(here, me, field, scope, params)).map((one: any) => [one.value, one.count])) test('★ each type is counted under the list’s other conditions, the type chosen left out', async () => { expect(await counts('type', null, { filters: [{ name: 'type', operator: 'in', value: ['main'] }, { name: 'state', operator: 'in', value: ['draft'] }] })).toEqual({ main: 1, sub: 0, popup: 1 }) expect(await counts('state', null, { search: 'line' })).toEqual({ draft: 1, released: 1 }) }) test('★ the scope narrows the counts as it narrows the list - one’s favorites, one’s own', async () => { expect(await counts('state', 'favor')).toEqual({ draft: 1, released: 0 }) expect(await counts('type', 'mywork')).toEqual({ main: 1, sub: 0, popup: 1 }) })