-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui-router): Convert to TypeScript and implement ui-router builder
- Loading branch information
Showing
13 changed files
with
486 additions
and
138 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 |
---|---|---|
@@ -1 +1,3 @@ | ||
**/*.js | ||
**/*.d.ts | ||
node_modules |
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 @@ | ||
**/*.ts | ||
**/*.tsx | ||
**/*.spec.* | ||
**/__mocks__/** | ||
**/__snapshots__/** |
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,4 @@ | ||
export const notification = { | ||
error: jest.fn(), | ||
info: jest.fn() | ||
} |
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,12 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`adds a URL redirection handler 1`] = `"The URL blank does not exist. You have been redirected to the home page."`; | ||
|
||
exports[`adds a default error handler 1`] = ` | ||
<div> | ||
<p> | ||
The following error was encountered during a UI state transition: | ||
</p> | ||
<code /> | ||
</div> | ||
`; |
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,13 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"target": "es6", | ||
"noImplicitAny": false, | ||
"sourceMap": false, | ||
"jsx": "react", | ||
"skipLibCheck": true | ||
}, | ||
"exclude": [ | ||
"node_modules" | ||
] | ||
} |
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,50 @@ | ||
import {buildRouter} from './ui-router'; | ||
import {notification} from 'antd'; | ||
import * as paramTypes from './ui-router/paramTypes'; | ||
|
||
it('adds a default error handler', async () => { | ||
var router = buildRouter(); | ||
|
||
router.stateRegistry.register({ | ||
name: 'badState', | ||
resolve: { | ||
willThrow: () => { | ||
throw 'whoops'; | ||
} | ||
} | ||
}); | ||
|
||
try { | ||
await router.stateService.go('badState'); | ||
} catch (e) {} | ||
|
||
expect(notification.error).toHaveBeenCalled(); | ||
var options = (notification.error as jest.Mock<any>).mock.calls[0][0]; | ||
|
||
expect(options.message).toBe('State Transition Error'); | ||
expect(options.description).toMatchSnapshot(); | ||
}); | ||
|
||
it('adds a URL redirection handler', () => { | ||
var router = buildRouter(); | ||
|
||
try { | ||
window.location.pathname = '/test'; | ||
router.urlRouter.sync(); | ||
} catch (e) {} | ||
|
||
expect(notification.info).toHaveBeenCalled(); | ||
var options = (notification.info as jest.Mock<any>).mock.calls[0][0]; | ||
|
||
expect(options.message).toBe('Unknown URL'); | ||
expect(options.description).toMatchSnapshot(); | ||
}); | ||
|
||
it('registers custom parameter type definitions', () => { | ||
var router = buildRouter(); | ||
|
||
var registeredTypes = Object.keys(router.urlMatcherFactory.paramTypes.types); | ||
var definedTypes = Object.keys(paramTypes); | ||
|
||
expect(registeredTypes).toEqual(definedTypes); | ||
}); |
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,45 @@ | ||
import * as React from 'react'; | ||
import {UIRouterReact, UIRouter, UIView, servicesPlugin, pushStateLocationPlugin} from 'ui-router-react'; | ||
import {ParamTypeDefinition, UrlParts} from 'ui-router-core'; | ||
import {UIRouterRx} from 'ui-router-rx'; | ||
import {notification} from 'antd'; | ||
import * as paramTypes from './ui-router/paramTypes'; | ||
|
||
export const buildRouter = (): UIRouterReact => { | ||
var router = new UIRouterReact(); | ||
|
||
router.plugin(servicesPlugin); | ||
router.plugin(pushStateLocationPlugin); | ||
router.plugin(UIRouterRx); | ||
|
||
router.stateService.defaultErrorHandler(err => { | ||
notification.error({ | ||
message: 'State Transition Error', | ||
|
||
description: ( | ||
<div> | ||
<p>The following error was encountered during a UI state transition:</p> | ||
<code>{err.stack}</code> | ||
</div> | ||
), | ||
|
||
duration: null | ||
}); | ||
}); | ||
|
||
router.urlRouter.otherwise((matchValue, url, router) => { | ||
notification.info({ | ||
message: 'Unknown URL', | ||
description: `The URL ${url.path} does not exist. You have been redirected to the home page.` | ||
}); | ||
|
||
return '/'; | ||
}); | ||
|
||
Object.keys(paramTypes).forEach(name => { | ||
router.urlMatcherFactory.type(name, paramTypes[name]); | ||
}); | ||
|
||
return router; | ||
} | ||
|
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,45 @@ | ||
import * as paramTypes from './paramTypes'; | ||
|
||
describe('where', () => { | ||
var obj = { | ||
user_id: '13', | ||
owner_id: '37', | ||
status: ['completed', 'failed'] | ||
}; | ||
|
||
var str = 'user_id:13!owner_id:37!~status:completed,failed'; | ||
|
||
it('encodes an object to a string', () => { | ||
expect(paramTypes.where.encode(obj)).toBe(str); | ||
}); | ||
|
||
it('decodes a string to an object', () => { | ||
expect(paramTypes.where.decode(str)).toEqual(obj); | ||
}); | ||
|
||
it('matches the regex pattern', () => { | ||
expect(str).toMatch(paramTypes.where.pattern); | ||
expect('test').not.toMatch(paramTypes.where.pattern); | ||
}); | ||
}); | ||
|
||
describe('order', () => { | ||
var obj = { | ||
created_at: 'desc' | ||
}; | ||
|
||
var str = 'created_at:desc'; | ||
|
||
it('encodes an object to a string', () => { | ||
expect(paramTypes.order.encode(obj)).toBe(str); | ||
}); | ||
|
||
it('decodes a string to an object', () => { | ||
expect(paramTypes.order.decode(str)).toEqual(obj); | ||
}); | ||
|
||
it('matches the regex pattern', () => { | ||
expect(str).toMatch(paramTypes.order.pattern); | ||
expect('test').not.toMatch(paramTypes.order.pattern); | ||
}); | ||
}); |
Oops, something went wrong.