|
| 1 | +import * as base64 from './base64-impl-browser' |
| 2 | +import { JSDOM } from 'jsdom' |
| 3 | + |
| 4 | +describe('base64-impl/browser', function () { |
| 5 | + if (+(process.version.match(/^v(\d+)/) as RegExpMatchArray)[1] < 8) { |
| 6 | + console.info('jsdom not supported, skipping base64-impl-browser...') |
| 7 | + return |
| 8 | + } |
| 9 | + |
| 10 | + beforeEach(function () { |
| 11 | + const dom = new JSDOM(``, { |
| 12 | + url: 'https://example.com/', |
| 13 | + contentType: 'text/html', |
| 14 | + includeNodeLocations: true |
| 15 | + }) |
| 16 | + |
| 17 | + // Mock btoa and atob on global object |
| 18 | + Object.defineProperty(global, 'btoa', { |
| 19 | + value: dom.window.btoa, |
| 20 | + writable: true, |
| 21 | + configurable: true |
| 22 | + }) |
| 23 | + Object.defineProperty(global, 'atob', { |
| 24 | + value: dom.window.atob, |
| 25 | + writable: true, |
| 26 | + configurable: true |
| 27 | + }) |
| 28 | + }) |
| 29 | + |
| 30 | + afterEach(function () { |
| 31 | + delete (global as any).btoa |
| 32 | + delete (global as any).atob |
| 33 | + }) |
| 34 | + |
| 35 | + describe('#base64Encode()', function () { |
| 36 | + it('should encode a simple string', function () { |
| 37 | + expect(base64.base64Encode('one two three')).toBe('b25lIHR3byB0aHJlZQ==') |
| 38 | + }) |
| 39 | + |
| 40 | + it('should encode an empty string', function () { |
| 41 | + expect(base64.base64Encode('')).toBe('') |
| 42 | + }) |
| 43 | + |
| 44 | + it('should encode a string with special characters', function () { |
| 45 | + expect(base64.base64Encode('Hello, World! @#$%')).toBe('SGVsbG8sIFdvcmxkISBAIyQl') |
| 46 | + }) |
| 47 | + |
| 48 | + it('should encode numeric strings', function () { |
| 49 | + expect(base64.base64Encode('123')).toBe('MTIz') |
| 50 | + }) |
| 51 | + |
| 52 | + it('should encode boolean strings', function () { |
| 53 | + expect(base64.base64Encode('true')).toBe('dHJ1ZQ==') |
| 54 | + }) |
| 55 | + }) |
| 56 | + |
| 57 | + describe('#base64Decode()', function () { |
| 58 | + it('should decode a simple string', function () { |
| 59 | + expect(base64.base64Decode('b25lIHR3byB0aHJlZQ==')).toBe('one two three') |
| 60 | + }) |
| 61 | + |
| 62 | + it('should decode an empty string', function () { |
| 63 | + expect(base64.base64Decode('')).toBe('') |
| 64 | + }) |
| 65 | + |
| 66 | + it('should decode a string with special characters', function () { |
| 67 | + expect(base64.base64Decode('SGVsbG8sIFdvcmxkISBAIyQl')).toBe('Hello, World! @#$%') |
| 68 | + }) |
| 69 | + |
| 70 | + it('should decode numeric strings', function () { |
| 71 | + expect(base64.base64Decode('MTIz')).toBe('123') |
| 72 | + }) |
| 73 | + |
| 74 | + it('should decode boolean strings', function () { |
| 75 | + expect(base64.base64Decode('dHJ1ZQ==')).toBe('true') |
| 76 | + }) |
| 77 | + }) |
| 78 | + |
| 79 | + describe('round-trip encoding/decoding', function () { |
| 80 | + it('should encode and decode back to original', function () { |
| 81 | + const original = 'Hello, World!' |
| 82 | + const encoded = base64.base64Encode(original) |
| 83 | + const decoded = base64.base64Decode(encoded) |
| 84 | + expect(decoded).toBe(original) |
| 85 | + }) |
| 86 | + |
| 87 | + it('should handle complex strings with special characters', function () { |
| 88 | + const original = 'Special chars: !@#$%^&*()_+-=[]{}|;:,.<>?' |
| 89 | + const encoded = base64.base64Encode(original) |
| 90 | + const decoded = base64.base64Decode(encoded) |
| 91 | + expect(decoded).toBe(original) |
| 92 | + }) |
| 93 | + |
| 94 | + it('should handle mixed unicode and ASCII', function () { |
| 95 | + const original = 'Hello 🌍' |
| 96 | + const encoded = base64.base64Encode(original) |
| 97 | + const decoded = base64.base64Decode(encoded) |
| 98 | + expect(decoded).toBe(original) |
| 99 | + }) |
| 100 | + }) |
| 101 | +}) |
0 commit comments