Skip to content

Commit

Permalink
test(utils/buffer): add tests for buffer.ts (#3004)
Browse files Browse the repository at this point in the history
* add test for equal function

* add test for uncovered branch

* remove unused import
  • Loading branch information
yasuaki640 authored Jun 23, 2024
1 parent 60d68ca commit f7a6b06
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/utils/buffer.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
import { SHA256 as sha256CryptoJS } from 'crypto-js'
import { bufferToFormData, bufferToString, timingSafeEqual } from './buffer'
import { bufferToFormData, bufferToString, equal, timingSafeEqual } from './buffer'

describe('equal', () => {
it('should return true for identical ArrayBuffers', () => {
const buffer1 = new ArrayBuffer(1)
const buffer2 = buffer1
expect(equal(buffer1, buffer2)).toBe(true)
})

it('should return false for ArrayBuffers of different lengths', () => {
const buffer1 = new ArrayBuffer(1)
const buffer2 = new ArrayBuffer(2)
expect(equal(buffer1, buffer2)).toBe(false)
})

it('should return false for ArrayBuffers with different content', () => {
const buffer1 = new Uint8Array([1, 2, 3, 4]).buffer
const buffer2 = new Uint8Array([2, 2, 3, 4]).buffer
expect(equal(buffer1, buffer2)).toBe(false)
})

it('should return true for ArrayBuffers with identical content', () => {
const buffer1 = new Uint8Array([1, 2, 3, 4]).buffer
const buffer2 = new Uint8Array([1, 2, 3, 4]).buffer
expect(equal(buffer1, buffer2)).toBe(true)
})
})

describe('buffer', () => {
it('positive', async () => {
Expand Down Expand Up @@ -43,6 +69,7 @@ describe('buffer', () => {
expect(await timingSafeEqual({ a: 1 }, { a: 2 })).toBe(false)
expect(await timingSafeEqual([1, 2], [1, 2])).toBe(false)
expect(await timingSafeEqual([1, 2], [1, 2, 3])).toBe(false)
expect(await timingSafeEqual('a', 'b', () => undefined)).toBe(false)
})
})

Expand All @@ -52,6 +79,10 @@ describe('bufferToString', () => {
const buffer = Uint8Array.from(bytes).buffer
expect(bufferToString(buffer)).toBe('あいうえお')
})
it('should return the passed arguments as is ', () => {
const notBuffer = 'あいうえお' as unknown as ArrayBuffer
expect(bufferToString(notBuffer)).toBe('あいうえお')
})
})

describe('bufferToFormData', () => {
Expand Down

0 comments on commit f7a6b06

Please sign in to comment.