-
-
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.
changed order of normalize and transform in load sprites
transformRequest is now called once with ResourceType.Sprite and not seperatly for SpiteJSON and SpriteImage. URL validation and appending of json/png is done afterwards
- Loading branch information
Showing
5 changed files
with
92 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,10 +26,10 @@ describe('normalizeSpriteURL', () => { | |
).toBe('http://www.foo.com/[email protected]?fresh=true'); | ||
}); | ||
|
||
test('test relative URL', () => { | ||
test('No Path', () => { | ||
expect( | ||
normalizeSpriteURL('/bar?fresh=true', '@2x', '.png') | ||
).toBe('/bar@2x.png?fresh=true'); | ||
normalizeSpriteURL('http://www.foo.com?fresh=true', '@2x', '.json') | ||
).toBe('http://www.foo.com/@2x.json?fresh=true'); | ||
}); | ||
}); | ||
|
||
|
@@ -66,9 +66,70 @@ describe('loadSprite', () => { | |
|
||
const result = await promise; | ||
|
||
expect(transform).toHaveBeenCalledTimes(2); | ||
expect(transform).toHaveBeenNthCalledWith(1, 'http://localhost:9966/test/unit/assets/sprite1.json', 'SpriteJSON'); | ||
expect(transform).toHaveBeenNthCalledWith(2, 'http://localhost:9966/test/unit/assets/sprite1.png', 'SpriteImage'); | ||
expect(transform).toHaveBeenCalledTimes(1); | ||
expect(transform).toHaveBeenNthCalledWith(1, 'http://localhost:9966/test/unit/assets/sprite1', 'Sprite'); | ||
|
||
expect(Object.keys(result)).toHaveLength(1); | ||
expect(Object.keys(result)[0]).toBe('default'); | ||
|
||
Object.values(result['default']).forEach(styleImage => { | ||
expect(styleImage.spriteData).toBeTruthy(); | ||
expect(styleImage.spriteData.context).toBeInstanceOf(CanvasRenderingContext2D); | ||
}); | ||
|
||
expect(server.requests[0].url).toBe('http://localhost:9966/test/unit/assets/sprite1.json'); | ||
expect(server.requests[1].url).toBe('http://localhost:9966/test/unit/assets/sprite1.png'); | ||
}); | ||
|
||
test('transform of relative url', async () => { | ||
const transform = jest.fn().mockImplementation((url, type) => { | ||
return {url: `http://localhost:9966${url}`, type}; | ||
}); | ||
|
||
const manager = new RequestManager(transform); | ||
|
||
server.respondWith('GET', 'http://localhost:9966/test/unit/assets/sprite1.json', fs.readFileSync(path.join(__dirname, '../../test/unit/assets/sprite1.json')).toString()); | ||
server.respondWith('GET', 'http://localhost:9966/test/unit/assets/sprite1.png', bufferToArrayBuffer(fs.readFileSync(path.join(__dirname, '../../test/unit/assets/sprite1.png')))); | ||
|
||
const promise = loadSprite('/test/unit/assets/sprite1', manager, 1, new AbortController()); | ||
|
||
server.respond(); | ||
|
||
const result = await promise; | ||
|
||
expect(transform).toHaveBeenCalledTimes(1); | ||
expect(transform).toHaveBeenNthCalledWith(1, '/test/unit/assets/sprite1', 'Sprite'); | ||
|
||
expect(Object.keys(result)).toHaveLength(1); | ||
expect(Object.keys(result)[0]).toBe('default'); | ||
|
||
Object.values(result['default']).forEach(styleImage => { | ||
expect(styleImage.spriteData).toBeTruthy(); | ||
expect(styleImage.spriteData.context).toBeInstanceOf(CanvasRenderingContext2D); | ||
}); | ||
|
||
expect(server.requests[0].url).toBe('http://localhost:9966/test/unit/assets/sprite1.json'); | ||
expect(server.requests[1].url).toBe('http://localhost:9966/test/unit/assets/sprite1.png'); | ||
}); | ||
|
||
test('transform of random Sprite String', async () => { | ||
const transform = jest.fn().mockImplementation((url, type) => { | ||
return {url: 'http://localhost:9966/test/unit/assets/sprite1', type}; | ||
}); | ||
|
||
const manager = new RequestManager(transform); | ||
|
||
server.respondWith('GET', 'http://localhost:9966/test/unit/assets/sprite1.json', fs.readFileSync(path.join(__dirname, '../../test/unit/assets/sprite1.json')).toString()); | ||
server.respondWith('GET', 'http://localhost:9966/test/unit/assets/sprite1.png', bufferToArrayBuffer(fs.readFileSync(path.join(__dirname, '../../test/unit/assets/sprite1.png')))); | ||
|
||
const promise = loadSprite('foobar', manager, 1, new AbortController()); | ||
|
||
server.respond(); | ||
|
||
const result = await promise; | ||
|
||
expect(transform).toHaveBeenCalledTimes(1); | ||
expect(transform).toHaveBeenNthCalledWith(1, 'foobar', 'Sprite'); | ||
|
||
expect(Object.keys(result)).toHaveLength(1); | ||
expect(Object.keys(result)[0]).toBe('default'); | ||
|
@@ -98,9 +159,8 @@ describe('loadSprite', () => { | |
|
||
const result = await promise; | ||
|
||
expect(transform).toHaveBeenCalledTimes(2); | ||
expect(transform).toHaveBeenNthCalledWith(1, '/test/unit/assets/sprite1.json', 'SpriteJSON'); | ||
expect(transform).toHaveBeenNthCalledWith(2, '/test/unit/assets/sprite1.png', 'SpriteImage'); | ||
expect(transform).toHaveBeenCalledTimes(1); | ||
expect(transform).toHaveBeenNthCalledWith(1, '/test/unit/assets/sprite1', 'Sprite'); | ||
|
||
expect(Object.keys(result)).toHaveLength(1); | ||
expect(Object.keys(result)[0]).toBe('default'); | ||
|
@@ -131,11 +191,9 @@ describe('loadSprite', () => { | |
server.respond(); | ||
|
||
const result = await promise; | ||
expect(transform).toHaveBeenCalledTimes(4); | ||
expect(transform).toHaveBeenNthCalledWith(1, 'http://localhost:9966/test/unit/assets/sprite1.json', 'SpriteJSON'); | ||
expect(transform).toHaveBeenNthCalledWith(2, 'http://localhost:9966/test/unit/assets/sprite1.png', 'SpriteImage'); | ||
expect(transform).toHaveBeenNthCalledWith(3, 'http://localhost:9966/test/unit/assets/sprite2.json', 'SpriteJSON'); | ||
expect(transform).toHaveBeenNthCalledWith(4, 'http://localhost:9966/test/unit/assets/sprite2.png', 'SpriteImage'); | ||
expect(transform).toHaveBeenCalledTimes(2); | ||
expect(transform).toHaveBeenNthCalledWith(1, 'http://localhost:9966/test/unit/assets/sprite1', 'Sprite'); | ||
expect(transform).toHaveBeenNthCalledWith(2, 'http://localhost:9966/test/unit/assets/sprite2', 'Sprite'); | ||
|
||
expect(Object.keys(result)).toHaveLength(2); | ||
expect(Object.keys(result)[0]).toBe('sprite1'); | ||
|
@@ -209,9 +267,8 @@ describe('loadSprite', () => { | |
server.respond(); | ||
|
||
const result = await promise; | ||
expect(transform).toHaveBeenCalledTimes(2); | ||
expect(transform).toHaveBeenNthCalledWith(1, 'http://localhost:9966/test/unit/assets/[email protected]', 'SpriteJSON'); | ||
expect(transform).toHaveBeenNthCalledWith(2, 'http://localhost:9966/test/unit/assets/[email protected]', 'SpriteImage'); | ||
expect(transform).toHaveBeenCalledTimes(1); | ||
expect(transform).toHaveBeenNthCalledWith(1, 'http://localhost:9966/test/unit/assets/sprite1', 'Sprite'); | ||
|
||
expect(Object.keys(result)).toHaveLength(1); | ||
expect(Object.keys(result)[0]).toBe('default'); | ||
|
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