-
Notifications
You must be signed in to change notification settings - Fork 746
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e94ed10
commit f8f1caa
Showing
2 changed files
with
38 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { atobUniversal, btoaUniversal } from '../../src/utils/encode'; | ||
|
||
describe('atobUniversal and btoaUniversal functions', () => { | ||
test('atobUniversal should decode base64 string to Uint8Array', () => { | ||
const base64 = 'SGVsbG8sIFdvcmxkIQ=='; // "Hello, World!" in base64 | ||
const expected = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]); | ||
const result = atobUniversal(base64); | ||
expect(result).toEqual(expected); | ||
}); | ||
|
||
test('btoaUniversal should encode ArrayBuffer to base64 string', () => { | ||
const { buffer } = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]); | ||
|
||
const expected = 'SGVsbG8sIFdvcmxkIQ=='; // "Hello, World!" in base64 | ||
const result = btoaUniversal(buffer); | ||
expect(result).toBe(expected); | ||
}); | ||
|
||
test('should round trip encode/decode correctly', () => { | ||
const originalString = 'Hello, World!'; | ||
const { buffer } = new TextEncoder().encode(originalString); | ||
const encoded = btoaUniversal(buffer); | ||
const decoded = new TextDecoder().decode(atobUniversal(encoded)); | ||
expect(decoded).toBe(originalString); | ||
}); | ||
|
||
test('should handle empty string', () => { | ||
const emptyBuffer = new ArrayBuffer(0); | ||
const encoded = btoaUniversal(emptyBuffer); | ||
expect(encoded).toBe(''); | ||
const decoded = atobUniversal(encoded); | ||
expect(decoded).toEqual(new Uint8Array([])); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters