Skip to content

Commit

Permalink
tests: improve tests (#33)
Browse files Browse the repository at this point in the history
* test(math): improve tests

* test(number): improve tests

* test(util): improve tests
  • Loading branch information
LoTwT authored Nov 19, 2024
1 parent 3a48b12 commit 6b680f1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions tests/math.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ it('minBy', () => {
it('maxBy', () => {
expect(maxBy([{ value: 1 }, { value: 2 }, { value: 3 }], (item) => item.value)).toEqual({ value: 3 })
expect(maxBy<{ value: number }>([], (item) => item.value)).toEqual(undefined)
expect(maxBy([{ value: 3 }, { value: 2 }, { value: 1 }], (item) => item.value)).toEqual({ value: 3 })
})

it('mean', () => {
Expand Down
4 changes: 4 additions & 0 deletions tests/number.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ it('randomNumber', () => {
expect(random).toBeGreaterThanOrEqual(min)
expect(random).toBeLessThanOrEqual(max)
expect(randomNumber(0, 0)).toBe(0)

const randomDefault = randomNumber()
expect(randomDefault).toBeGreaterThanOrEqual(0)
expect(randomDefault).toBeLessThanOrEqual(100)
})

it('times', () => {
Expand Down
14 changes: 13 additions & 1 deletion tests/util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ describe('copyText', () => {

it('download', () => {
let href = ''
let filename = ''

Reflect.defineProperty(HTMLAnchorElement.prototype, 'href', {
set(v) {
Expand All @@ -277,13 +278,24 @@ it('download', () => {
},
})

Reflect.defineProperty(HTMLAnchorElement.prototype, 'download', {
set(v) {
filename = v
},
get() {
return filename
},
})

URL.createObjectURL = vi.fn(() => 'mock')
URL.revokeObjectURL = vi.fn()
download(new Blob(['hello']), 'test.txt')
expect(href).toBe('mock')
expect(filename).toBe('test.txt')
expect(URL.createObjectURL).toHaveBeenCalled()
expect(URL.revokeObjectURL).toHaveBeenCalled()

download('/a.jpg', 'test.txt')
download('/a.jpg')
expect(href).toBe('/a.jpg')
expect(filename).toBe('file')
})

0 comments on commit 6b680f1

Please sign in to comment.