From 1330d0d6b60a2e60fb82362763dd5589e6158dd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carsten=20K=C3=B6nig?= Date: Tue, 23 Jul 2024 21:32:18 +0200 Subject: [PATCH] adds missing spec --- package.json | 1 + tests/unit/helpers/filter.spec.ts | 100 ++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 tests/unit/helpers/filter.spec.ts diff --git a/package.json b/package.json index d1a513e9..355cf2c3 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "tsc": "tsc --noEmit && vue-tsc", "lint": "eslint src", "test:unit": "vitest run tests/unit", + "test:unit:watch": "vitest watch tests/unit", "test:e2e": "playwright test --project=chromium", "test:e2e:all": "playwright test", "test:e2e:watch": "PWTEST_WATCH=1 playwright test --project=chromium", diff --git a/tests/unit/helpers/filter.spec.ts b/tests/unit/helpers/filter.spec.ts new file mode 100644 index 00000000..892c301a --- /dev/null +++ b/tests/unit/helpers/filter.spec.ts @@ -0,0 +1,100 @@ +import { describe, it, expect } from 'vitest' + +import { filterItems } from '../../../src/helpers/filters' + +const items = [ + { name: 'x-wing', type: 'ship' }, + { name: 'darth vader', type: 'person' }, + { name: 'milenium falcon', type: 'ship' }, + { name: 'luke skywalker', type: 'person' }, + { name: 'tatooine', type: 'location' }, + { name: 'qui-gon jin', type: 'person' }, + { name: 'coruscant', type: 'location' }, + { name: 'yoda', type: 'person' }, + { name: 'dagobah', type: 'location' }, + { name: 'starfighter', type: 'ship' }, + { name: null, type: null }, + { name: null, type: 'weapon' }, + { name: 'grogu', type: null }, + { age: 5 }, + { obj: { _id: -1, id: 1.2, type: undefined, real: false } } +] +const headerNames = ['name', 'type'] + +describe.concurrent('helpers/filters.ts', () => { + describe.concurrent('filters.ts default searches', () => { + it('should return all items on empty search', () => { + const results = filterItems(items, '', headerNames) + expect(results).toEqual(items) + }) + + it('should return all items on whitespace search', () => { + const results = filterItems(items, ' ', headerNames) + expect(results).toEqual(items) + }) + + it('should return no items on unmatched search', () => { + const results = filterItems(items, 'xxxxxxxxxxxxxx', headerNames) + expect(results).toEqual([]) + }) + + it('should return all persons on person search', () => { + const persons = items.filter(item => item.type === 'person') + const results = filterItems(items, 'person', headerNames) + + expect(results).toEqual(persons) + }) + + it('should match on "da" search', () => { + const results = filterItems(items, 'da', headerNames) + const resultNames = results.map(result => result.name) + const expected = ['dagobah', 'darth vader', 'yoda'] + expect(resultNames).toEqual(expect.arrayContaining(expected)) + }) + + it('should match on "dar" search', () => { + const results = filterItems(items, 'dar', headerNames) + const resultNames = results.map(result => result.name) + const expected = ['darth vader'] + expect(resultNames).toEqual(expect.arrayContaining(expected)) + }) + + it('should filter all things on s search', () => { + const expected = ['milenium falcon', 'x-wing', 'starfighter', 'luke skywalker', 'darth vader', 'yoda', 'qui-gon jin', 'coruscant'] + + const results = filterItems(items, 's', headerNames) + const resultNames = results.map(result => result.name) + + expect(resultNames).toEqual(expect.arrayContaining(expected)) + }) + }) + + describe.concurrent('filters.ts column searches', () => { + it('should return all items on empty column search', () => { + const results = filterItems(items, 'type:', headerNames) + expect(results).toEqual(items) + }) + + it('should return all items on whitespace column search', () => { + const results = filterItems(items, 'type: ', headerNames) + expect(results).toEqual(items) + }) + + it('should return all persons on type:person search', () => { + const persons = items.filter(item => item.type === 'person') + const results = filterItems(items, 'type:person', headerNames) + + expect(results).toEqual(persons) + }) + + it('should filter all ships and persons on type:s search', () => { + const shipsAndPersons = items.filter(item => ['ship', 'person'].includes(item.type)) + const itemNames = shipsAndPersons.map(item => item.name) + + const results = filterItems(items, 'type:s', headerNames) + const resultNames = results.map(result => result.name) + + expect(resultNames).toEqual(expect.arrayContaining(itemNames)) + }) + }) +}) \ No newline at end of file