-
-
Notifications
You must be signed in to change notification settings - Fork 717
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for RTL text plugin 0.3.0 (#4860)
* Allow loading rtl plugin async * Allow loading rtl plugin async * Add changelog * Fix typecheck and code * Add tests to make sure both 0.2.3 and 0.3.0 are supported * Improve test name * Change to use latest version everywhere. * Revert unrelated changes
- Loading branch information
Showing
9 changed files
with
181 additions
and
139 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
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,118 @@ | ||
import {PluginState} from './rtl_text_plugin_status'; | ||
import {rtlWorkerPlugin} from './rtl_text_plugin_worker'; | ||
|
||
describe('RTLWorkerPlugin', () => { | ||
beforeEach(() => { | ||
// This is a static class, so we need to reset the properties before each test | ||
rtlWorkerPlugin.processStyledBidirectionalText = null; | ||
rtlWorkerPlugin.processBidirectionalText = null; | ||
rtlWorkerPlugin.applyArabicShaping = null; | ||
}); | ||
|
||
test('should throw if already parsed', () => { | ||
const rtlTextPlugin = { | ||
applyArabicShaping: jest.fn(), | ||
processBidirectionalText: jest.fn(), | ||
processStyledBidirectionalText: jest.fn(), | ||
}; | ||
|
||
rtlWorkerPlugin.setMethods(rtlTextPlugin); | ||
expect(() => { | ||
rtlWorkerPlugin.setMethods(rtlTextPlugin); | ||
}).toThrow('RTL text plugin already registered.'); | ||
}); | ||
|
||
test('should move RTL plugin from unavailable to deferred', async () => { | ||
rtlWorkerPlugin.pluginURL = ''; | ||
rtlWorkerPlugin.pluginStatus = 'unavailable'; | ||
|
||
const mockMessage: PluginState = { | ||
pluginURL: 'https://somehost/somescript', | ||
pluginStatus: 'deferred' | ||
}; | ||
|
||
await rtlWorkerPlugin.syncState(mockMessage, jest.fn()); | ||
|
||
expect(rtlWorkerPlugin.getRTLTextPluginStatus()).toBe('deferred'); | ||
}); | ||
|
||
test('should not change RTL plugin status if already parsed', async () => { | ||
const originalUrl = 'https://somehost/somescript1'; | ||
rtlWorkerPlugin.pluginURL = originalUrl; | ||
rtlWorkerPlugin.pluginStatus = 'loaded'; | ||
rtlWorkerPlugin.setMethods({ | ||
applyArabicShaping: jest.fn(), | ||
processBidirectionalText: jest.fn(), | ||
processStyledBidirectionalText: jest.fn(), | ||
}); | ||
const mockMessage: PluginState = { | ||
pluginURL: 'https://somehost/somescript2', | ||
pluginStatus: 'loading' | ||
}; | ||
|
||
const workerResult: PluginState = await await rtlWorkerPlugin.syncState(mockMessage, jest.fn()); | ||
|
||
expect(rtlWorkerPlugin.getRTLTextPluginStatus()).toBe('loaded'); | ||
expect(rtlWorkerPlugin.pluginURL).toBe(originalUrl); | ||
|
||
expect(workerResult.pluginStatus).toBe('loaded'); | ||
expect(workerResult.pluginURL).toBe(originalUrl); | ||
}); | ||
|
||
test('should do a full cycle of rtl loading synchronously', async () => { | ||
const originalUrl = 'https://somehost/somescript1'; | ||
const loadScriptsMock = jest.fn().mockImplementation((_) => { | ||
rtlWorkerPlugin.setMethods({ | ||
applyArabicShaping: jest.fn(), | ||
processBidirectionalText: jest.fn(), | ||
processStyledBidirectionalText: jest.fn(), | ||
}); | ||
}); | ||
|
||
const workerResult: PluginState = await rtlWorkerPlugin.syncState({ | ||
pluginURL: originalUrl, | ||
pluginStatus: 'loading' | ||
}, loadScriptsMock); | ||
|
||
expect(rtlWorkerPlugin.getRTLTextPluginStatus()).toBe('loaded'); | ||
expect(rtlWorkerPlugin.pluginURL).toBe(originalUrl); | ||
expect(workerResult.pluginStatus).toBe('loaded'); | ||
expect(workerResult.pluginURL).toBe(originalUrl); | ||
}); | ||
|
||
test('should do a full cycle of rtl loading asynchronously', async () => { | ||
const originalUrl = 'https://somehost/somescript1'; | ||
const loadScriptsMock = jest.fn().mockImplementation((_) => { | ||
setTimeout(() => { | ||
rtlWorkerPlugin.setMethods({ | ||
applyArabicShaping: jest.fn(), | ||
processBidirectionalText: jest.fn(), | ||
processStyledBidirectionalText: jest.fn(), | ||
}); | ||
}, 10); | ||
}); | ||
|
||
const workerResult: PluginState = await rtlWorkerPlugin.syncState({ | ||
pluginURL: originalUrl, | ||
pluginStatus: 'loading' | ||
}, loadScriptsMock); | ||
|
||
expect(rtlWorkerPlugin.getRTLTextPluginStatus()).toBe('loaded'); | ||
expect(rtlWorkerPlugin.pluginURL).toBe(originalUrl); | ||
expect(workerResult.pluginStatus).toBe('loaded'); | ||
expect(workerResult.pluginURL).toBe(originalUrl); | ||
}); | ||
|
||
test('should fail loading on timeout', async () => { | ||
const originalUrl = 'https://somehost/somescript1'; | ||
const loadScriptsMock = jest.fn().mockImplementation(() => {}); | ||
|
||
(rtlWorkerPlugin as any).TIMEOUT = 1; | ||
|
||
await expect(rtlWorkerPlugin.syncState({ | ||
pluginURL: originalUrl, | ||
pluginStatus: 'loading' | ||
}, loadScriptsMock) | ||
).rejects.toThrow('RTL Text Plugin failed to import scripts from https://somehost/somescript1'); | ||
}); | ||
}); |
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
Oops, something went wrong.