-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: font store load method skip loading (#1909)
- Loading branch information
Showing
4 changed files
with
55 additions
and
8 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,5 @@ | ||
--- | ||
'@react-pdf/font': patch | ||
--- | ||
|
||
fix: font store load method skip loading |
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
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
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,41 @@ | ||
const path = require('path'); | ||
const fontkit = require('@react-pdf/fontkit'); | ||
const { default: FontStore } = require('../src/index'); | ||
|
||
global.BROWSER = false; | ||
|
||
describe('FontStore', () => { | ||
test('load method will wait for font loaded when parallel execution', async () => { | ||
const openSpy = jest.spyOn(fontkit.default, 'open'); | ||
const fontStore = new FontStore(); | ||
const fontFamily = '思源'; | ||
fontStore.register({ | ||
family: fontFamily, // assume this is a chinese font | ||
fonts: [ | ||
{ | ||
src: path.join(__dirname, '../../examples/public/Roboto-Bold.ttf'), | ||
}, | ||
], | ||
}); | ||
|
||
const getFontSource = () => | ||
fontStore.getRegisteredFonts()[fontFamily].sources[0]; | ||
|
||
const getFontData = () => getFontSource().data; | ||
|
||
const makeSureFontLoadedAfterLoad = async () => { | ||
expect(getFontData()).toBeNull(); | ||
await fontStore.load({ | ||
fontFamily, | ||
}); | ||
expect(getFontData()).not.toBeNull(); | ||
}; | ||
return Promise.all([ | ||
makeSureFontLoadedAfterLoad(), | ||
makeSureFontLoadedAfterLoad(), | ||
]).then(() => { | ||
// call load method twice but just call file open method once | ||
expect(openSpy.mock.calls).toHaveLength(1); | ||
}); | ||
}); | ||
}); |