From fc240bbae645618cc7e5eae55c77d1bb6193f7fc Mon Sep 17 00:00:00 2001 From: Xyphyn Date: Sat, 8 Jul 2023 15:10:59 -0700 Subject: [PATCH] feat: Add some more tests --- tests/formattednumber.test.ts | 47 +++++++++++++++++++++++++++++++++++ tests/lemmy.test.ts | 12 +++++++++ tests/relativedate.test.ts | 8 +++--- 3 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 tests/formattednumber.test.ts create mode 100644 tests/lemmy.test.ts diff --git a/tests/formattednumber.test.ts b/tests/formattednumber.test.ts new file mode 100644 index 00000000..e8a09765 --- /dev/null +++ b/tests/formattednumber.test.ts @@ -0,0 +1,47 @@ +import { tick } from 'svelte' +import FormattedNumber from '../src/lib/components/util/FormattedNumber.svelte' +import { describe, expect, it } from 'vitest' + +describe('FormattedNumber.svelte', () => { + let host = document.createElement('div') + host.setAttribute('id', 'host') + document.body.appendChild(host) + + it('mounts', () => { + const instance = new FormattedNumber({ + target: host, + props: { + number: 1000, + }, + }) + expect(instance).toBeTruthy() + expect(host.innerHTML).toBeTruthy() + }) + it('formats numbers correctly', async () => { + const instance = new FormattedNumber({ + target: host, + props: { + number: 1100, + }, + }) + expect(instance).toBeTruthy() + + expect(host.innerHTML).toContain('1.1K') + + instance.$set({ number: 5000 }) + await tick() + expect(host.innerHTML).toContain('5K') + + instance.$set({ number: 6_320_000 }) + await tick() + expect(host.innerHTML).toContain('6.3M') + + instance.$set({ + options: { + notation: 'standard', + }, + }) + await tick() + expect(host.innerHTML).toContain('6,320,000') + }) +}) diff --git a/tests/lemmy.test.ts b/tests/lemmy.test.ts new file mode 100644 index 00000000..cf6d051e --- /dev/null +++ b/tests/lemmy.test.ts @@ -0,0 +1,12 @@ +import { isImage } from '../src/lib/ui/image.js' +import { describe, expect, it } from 'vitest' + +describe('lemmy utilities', () => { + it('checks image URLs correctly', () => { + expect(isImage(`https://example.com/img.jpg?thumb=28`)).toBeTruthy() + expect(isImage(`https://test.dev/file/img.webp?great`)).toBeTruthy() + expect(isImage(`https://test.dev/file/img.jpeg?test=false`)).toBeTruthy() + expect(isImage(`https://test.dev/file/img.bmp#awesome`)).toBeTruthy() + expect(isImage(`https://test.dev/file/img.png#awesome`)).toBeTruthy() + }) +}) diff --git a/tests/relativedate.test.ts b/tests/relativedate.test.ts index 812ac16a..bffd7107 100644 --- a/tests/relativedate.test.ts +++ b/tests/relativedate.test.ts @@ -6,6 +6,9 @@ describe('RelativeDate.svelte', () => { host.setAttribute('id', 'host') document.body.appendChild(host) + const testDate = new Date('2023-01-01 00:00:00') + const testRelativeTo = new Date('2023-01-01 01:00:00') + it('mounts', () => { let host = document.createElement('div') const instance = new RelativeDate({ @@ -18,12 +21,9 @@ describe('RelativeDate.svelte', () => { expect(instance).toBeTruthy() }) it('shows the correct date', () => { - const date = new Date('2023-01-01 00:00:00') - const relativeTo = new Date('2023-01-01 01:00:00') - const instance = new RelativeDate({ target: host, - props: { date: date, relativeTo: relativeTo }, + props: { date: testDate, relativeTo: testRelativeTo }, }) expect(instance).toBeTruthy()