File size: 2,585 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
import { stripFormatSpecifiers } from './receive-logs'
describe('stripFormatSpecifiers', () => {
it('should only process when first arg is string containing %', () => {
expect(stripFormatSpecifiers([])).toEqual([])
expect(stripFormatSpecifiers([123])).toEqual([123])
expect(stripFormatSpecifiers(['no percent'])).toEqual(['no percent'])
})
it('should replace format specifiers with their arguments', () => {
expect(stripFormatSpecifiers(['%s', 'string'])).toEqual(['string'])
expect(stripFormatSpecifiers(['Hello %s', 'world'])).toEqual([
'Hello world',
])
expect(stripFormatSpecifiers(['%d', 42])).toEqual(['42'])
expect(stripFormatSpecifiers(['%i', 123])).toEqual(['123'])
expect(stripFormatSpecifiers(['%f', 3.14])).toEqual(['3.14'])
expect(stripFormatSpecifiers(['%o', { a: 1 }])).toEqual(['{ a: 1 }'])
expect(stripFormatSpecifiers(['%O', { a: 1 }])).toEqual(['{ a: 1 }'])
expect(stripFormatSpecifiers(['%j', { a: 1 }])).toEqual(['{ a: 1 }'])
})
it('should strip CSS styling from %c format specifier', () => {
expect(stripFormatSpecifiers(['%c', 'css'])).toEqual([''])
expect(stripFormatSpecifiers(['%cStyled text', 'color: red'])).toEqual([
'Styled text',
])
expect(
stripFormatSpecifiers(['%cError: %s', 'color: red', 'Something failed'])
).toEqual(['Error: Something failed'])
expect(
stripFormatSpecifiers(['%cRed %cBlue', 'color: red', 'color: blue'])
).toEqual(['Red Blue'])
expect(
stripFormatSpecifiers([
'%cDownload the React DevTools for a better development experience: https://react.dev/link/react-devtools',
'font-weight:bold',
])
).toEqual([
'Download the React DevTools for a better development experience: https://react.dev/link/react-devtools',
])
expect(
stripFormatSpecifiers([
'%cStyled %s text %d',
'color: red',
'interpolated',
42,
])
).toEqual(['Styled interpolated text 42'])
})
it('should handle escaped percent signs', () => {
expect(stripFormatSpecifiers(['%%'])).toEqual(['%'])
expect(stripFormatSpecifiers(['100%%', 'unused'])).toEqual([
'100%',
'unused',
])
})
it('should preserve excess arguments after all specifiers consumed', () => {
expect(stripFormatSpecifiers(['%s', 'used', 'excess1', 'excess2'])).toEqual(
['used', 'excess1', 'excess2']
)
})
it('should handle % at end of string', () => {
expect(stripFormatSpecifiers(['ends with %'])).toEqual(['ends with %'])
})
})
|