forked from auth0/react-native-auth0
-
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.
Merge pull request auth0#81 from auth0/added-link-dev-improvements
Return error if native module is not linked
- Loading branch information
Showing
7 changed files
with
187 additions
and
6 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 |
---|---|---|
|
@@ -237,4 +237,6 @@ gen-external-apklibs | |
|
||
# Release process | ||
.release | ||
.release-tmp-*/ | ||
.release-tmp-*/ | ||
|
||
.project |
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,27 @@ | ||
export default class A0Auth0 { | ||
showUrl(url, closeOnLoad, callback) { | ||
this.url = url; | ||
this.hidden = false; | ||
if (this.error || closeOnLoad) { | ||
callback(this.error); | ||
} else { | ||
this.onUrl(); | ||
} | ||
} | ||
|
||
hide() { | ||
this.hidden = true; | ||
} | ||
|
||
reset() { | ||
this.url = null; | ||
this.error = null; | ||
this.hidden = true; | ||
this.parameters = null; | ||
this.onUrl = () => {}; | ||
} | ||
|
||
oauthParameters(callback) { | ||
callback(this.parameters); | ||
} | ||
} |
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,17 @@ | ||
import EventEmitter from 'events'; | ||
|
||
class CustomEmitter extends EventEmitter {} | ||
|
||
export default class Linking { | ||
constructor() { | ||
this.emitter = new CustomEmitter(); | ||
} | ||
|
||
addEventListener(event, fn) { | ||
this.emitter.addListener(event, fn); | ||
} | ||
|
||
removeEventListener(event, fn) { | ||
this.emitter.removeListener(event, 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,9 @@ | ||
import Linking from './linking'; | ||
import A0Auth0 from './auth0'; | ||
|
||
const mock = {}; | ||
|
||
mock.Linking = new Linking(); | ||
mock.NativeModules = { A0Auth0: new A0Auth0() }; | ||
|
||
module.exports = mock; |
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,15 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Agent newTransaction should call native integration 1`] = ` | ||
Object { | ||
"state": "state", | ||
} | ||
`; | ||
|
||
exports[`Agent newTransaction should fail if native module is not linked 1`] = `[Error: Missing NativeModule. Please make sure you run \`react-native link react-native-auth0\`]`; | ||
|
||
exports[`Agent show complete web flow should resolve promise with url result 1`] = `"https://auth0.com"`; | ||
|
||
exports[`Agent show failed flow should reject with error 1`] = `[Error: failed to load]`; | ||
|
||
exports[`Agent show should fail if native module is not linked 1`] = `[Error: Missing NativeModule. Please make sure you run \`react-native link react-native-auth0\`]`; |
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,105 @@ | ||
jest.mock('react-native'); | ||
import Agent from '../agent'; | ||
import { NativeModules, Linking } from 'react-native'; | ||
const A0Auth0 = NativeModules.A0Auth0; | ||
|
||
describe('Agent', () => { | ||
const agent = new Agent(); | ||
|
||
describe('show', () => { | ||
|
||
beforeEach(() => { | ||
NativeModules.A0Auth0 = A0Auth0; | ||
A0Auth0.reset(); | ||
}); | ||
|
||
it('should fail if native module is not linked', async () => { | ||
NativeModules.A0Auth0 = undefined; | ||
expect.assertions(1); | ||
await expect(agent.show('https://auth0.com')).rejects.toMatchSnapshot(); | ||
}); | ||
|
||
describe('complete web flow', () => { | ||
beforeEach(() => { | ||
A0Auth0.onUrl = () => { | ||
Linking.emitter.emit('url', {url: 'https://auth0.com'}); | ||
}; | ||
}); | ||
|
||
it('should resolve promise with url result', async () => { | ||
expect.assertions(1); | ||
await expect(agent.show('https://auth0.com')).resolves.toMatchSnapshot(); | ||
}); | ||
|
||
it('should show initial url', async () => { | ||
expect.assertions(1); | ||
const url = 'https://auth0.com/authorize'; | ||
await agent.show(url); | ||
expect(A0Auth0.url).toEqual(url); | ||
}); | ||
}); | ||
|
||
describe('listeners', () => { | ||
|
||
it('should register url listeners', () => { | ||
A0Auth0.onUrl = () => {}; | ||
const url = 'https://auth0.com/authorize'; | ||
agent.show(url); | ||
expect(Linking.emitter.listenerCount('url')).toEqual(1); | ||
}); | ||
|
||
it('should remove url listeners when done', async () => { | ||
A0Auth0.onUrl = () => { | ||
Linking.emitter.emit('url', {url: 'https://auth0.com'}); | ||
}; | ||
expect.assertions(1); | ||
const url = 'https://auth0.com/authorize'; | ||
await agent.show(url); | ||
expect(Linking.emitter.listenerCount('url')).toEqual(0); | ||
}); | ||
|
||
it('should remove url listeners when load fails', async () => { | ||
expect.assertions(1); | ||
A0Auth0.error = new Error('failed to load'); | ||
const url = 'https://auth0.com/authorize'; | ||
await agent.show(url).catch((err) => Promise.resolve(err)); | ||
expect(Linking.emitter.listenerCount('url')).toEqual(0); | ||
}); | ||
|
||
it('should remove url listeners on first load', async () => { | ||
expect.assertions(1); | ||
const url = 'https://auth0.com/authorize'; | ||
await agent.show(url, true); | ||
expect(Linking.emitter.listenerCount('url')).toEqual(0); | ||
}); | ||
|
||
}); | ||
|
||
describe('failed flow', () => { | ||
|
||
it('should reject with error', async () => { | ||
expect.assertions(1); | ||
A0Auth0.error = new Error('failed to load'); | ||
await expect(agent.show('https://auth0.com')).rejects.toMatchSnapshot(); | ||
}); | ||
|
||
}); | ||
}); | ||
|
||
describe('newTransaction', () => { | ||
|
||
it('should call native integration', async () => { | ||
const parameters = {state: 'state'}; | ||
A0Auth0.parameters = parameters; | ||
expect.assertions(1); | ||
await expect(agent.newTransaction()).resolves.toMatchSnapshot(); | ||
}); | ||
|
||
it('should fail if native module is not linked', async () => { | ||
NativeModules.A0Auth0 = undefined; | ||
expect.assertions(1); | ||
await expect(agent.newTransaction()).rejects.toMatchSnapshot(); | ||
}); | ||
|
||
}); | ||
}); |
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