|
| 1 | +import { Font } from './font'; |
| 2 | + |
| 3 | +describe('Font', () => { |
| 4 | + describe('cloneOrDirty', () => { |
| 5 | + // simulate the android cached typeface set by Font.getAndroidTypeface() |
| 6 | + const fontWithCachedTypeface = () => { |
| 7 | + const font = new Font('serif', 12, 'normal', 'normal'); |
| 8 | + font['_typeface'] = { cached: true }; |
| 9 | + return font; |
| 10 | + }; |
| 11 | + |
| 12 | + it('clones do not carry the cached typeface', () => { |
| 13 | + const font = fontWithCachedTypeface(); |
| 14 | + const clones = [font.withFontWeight('700'), font.withFontFamily('monospace'), font.withFontStyle('italic'), font.withFontSize(20), font.withFontScale(2), font.withFontVariationSettings([{ axis: 'wght', value: 700 }])]; |
| 15 | + for (const clone of clones) { |
| 16 | + expect(clone).not.toBe(font); |
| 17 | + expect(clone['_typeface']).toBeNull(); |
| 18 | + } |
| 19 | + // source font keeps its own cache |
| 20 | + expect(font['_typeface']).toEqual({ cached: true }); |
| 21 | + }); |
| 22 | + |
| 23 | + it('clones keep the font descriptor and apply the new property', () => { |
| 24 | + const clone = fontWithCachedTypeface().withFontWeight('700'); |
| 25 | + expect(clone.fontFamily).toBe('serif'); |
| 26 | + expect(clone.fontSize).toBe(12); |
| 27 | + expect(clone.fontStyle).toBe('normal'); |
| 28 | + expect(clone.fontWeight).toBe('700'); |
| 29 | + }); |
| 30 | + |
| 31 | + it('dirtied (non cloned) font clears the cached typeface', () => { |
| 32 | + const font = fontWithCachedTypeface(); |
| 33 | + const result = font.withFontWeight('700', false); |
| 34 | + expect(result).toBe(font); |
| 35 | + expect(result.isDirty).toBe(true); |
| 36 | + expect(result['_typeface']).toBeNull(); |
| 37 | + }); |
| 38 | + }); |
| 39 | +}); |
0 commit comments