Skip to content

Commit d530de6

Browse files
add tests for floatToPixel (#909)
1 parent ffbb8ef commit d530de6

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/transformers/floatToPixel.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import {getMockToken} from '../test-utilities'
2+
import {floatToPixel} from './floatToPixel'
3+
4+
describe('Transformer: floatToPixel', () => {
5+
it('transforms float to pixel', () => {
6+
const input = [
7+
getMockToken({
8+
value: 1.5,
9+
$extensions: {
10+
'org.primer.data': {
11+
fontSize: 16,
12+
},
13+
},
14+
}),
15+
]
16+
const expectedOutput = ['24px']
17+
expect(input.map(item => floatToPixel.transformer(item, {}))).toStrictEqual(expectedOutput)
18+
})
19+
20+
it('transforms 0 to 0', () => {
21+
const input = [
22+
getMockToken({
23+
value: 0,
24+
$extensions: {
25+
'org.primer.data': {
26+
fontSize: 16,
27+
},
28+
},
29+
}),
30+
getMockToken({
31+
value: 0,
32+
}),
33+
]
34+
const expectedOutput = [0, 0]
35+
expect(input.map(item => floatToPixel.transformer(item, {}))).toStrictEqual(expectedOutput)
36+
})
37+
38+
it('ignore invalid', () => {
39+
const input = [
40+
getMockToken({
41+
value: '1.5',
42+
$extensions: {
43+
'org.primer.data': {
44+
fontSize: 16,
45+
},
46+
},
47+
}),
48+
getMockToken({
49+
value: 1.5,
50+
}),
51+
getMockToken({
52+
value: 2,
53+
$extensions: {
54+
'org.primer.data': {},
55+
},
56+
}),
57+
]
58+
const expectedOutput = ['1.5', 1.5, 2]
59+
expect(input.map(item => floatToPixel.transformer(item, {}))).toStrictEqual(expectedOutput)
60+
})
61+
})

src/transformers/floatToPixel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const convertFloatToPixel = (token: StyleDictionary.TransformedToken) =>
1717
// convert value
1818
const convertedValue = token.$extensions?.['org.primer.data']?.fontSize * token.value
1919
// return converted value
20-
return `${convertedValue}px`
20+
return convertedValue === 0 ? 0 : `${convertedValue}px`
2121
}
2222
/**
2323
* @description converts a float value to a pixel value based on the provided fontSize on the tokersn extension

0 commit comments

Comments
 (0)