Skip to content
This repository has been archived by the owner on May 30, 2022. It is now read-only.

Commit

Permalink
Add 100% test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Flores committed Feb 14, 2017
1 parent 260873d commit 11216b5
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 28 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -14,3 +14,6 @@
#
node_modules/
npm-debug.log

# Jest coverage
coverage/
14 changes: 10 additions & 4 deletions package.json
Expand Up @@ -10,11 +10,17 @@
"test": "jest"
},
"jest": {
"preset": "react-native"
"preset": "react-native",
"collectCoverage": true,
"coverageThreshold": {
"global": {
"branches": 100,
"functions": 100,
"lines": 100,
"statements": 100
}
}
},
"transformIgnorePatterns": [
"node_modules/(?!react-native|my-project|react-native-button)"
],
"author": "Luis Flores <me@luisflores.mx> (http://luisflores.mx/)",
"license": "MIT",
"dependencies": {
Expand Down
25 changes: 10 additions & 15 deletions src/index.js
Expand Up @@ -9,7 +9,7 @@ const fetchQueries = (expression) => {

let match;
while (match = regex.exec(expression)) {
if (match && match[0]) {
if (match && match[0] && match[1]) {
queries.push(match[0]);
}
}
Expand All @@ -26,7 +26,7 @@ const execRegex = (queries, expression, path) => {
const queryRegex = new RegExp(regexExpression, 'g');
const match = queryRegex.exec(path);

if (match) {
if (match && !match[1].includes('/')) {
let results = { path: match[0] }
queries.forEach((query, index) => {
const id = query.substring(1);
Expand All @@ -39,16 +39,6 @@ const execRegex = (queries, expression, path) => {
return false;
};

const evaluateRoute = (expression, path) => {
const queries = fetchQueries(expression);

if (queries.length) {
return execRegex(queries, expression, path);
}

return false;
};

const evaluateExpression = (expression, path, scheme) => {
if (expression === path) {
return { scheme, path };
Expand All @@ -64,7 +54,10 @@ const evaluateExpression = (expression, path, scheme) => {
}

if (typeof expression === 'string' && expression.includes(':')) {
return evaluateRoute(expression, path);
const queries = fetchQueries(expression);
if (queries.length) {
return execRegex(queries, expression, path);
}
}

return false;
Expand Down Expand Up @@ -102,10 +95,12 @@ const addScheme = (scheme) => {
};

const handleUrl = ({ url }) => {
Linking.canOpenURL(url).then((supported) => {
return Linking.canOpenURL(url).then((supported) => {
if (supported) {
evaluateUrl(url);
evaluateUrl(url);
}

return Promise.resolve(supported);
});
};

Expand Down
89 changes: 80 additions & 9 deletions test/index.test.js
@@ -1,5 +1,7 @@
import DeepLinking, { evaluateUrl } from '../src';

DeepLinking.addScheme('domain://');

describe('DeepLinking', () => {
const articleRoute = {
expression: 'domain://article',
Expand All @@ -10,14 +12,16 @@ describe('DeepLinking', () => {
expression: 'domain://music',
callback: () => {},
};


beforeEach(() => {
expect(DeepLinking.routes).toEqual([]);
});

afterEach(() => {
DeepLinking.resetRoutes();
});

test('addRoute', () => {
expect(DeepLinking.routes).toEqual([]);

DeepLinking.addRoute(articleRoute.expression, articleRoute.callback);
expect(DeepLinking.routes).toEqual([articleRoute]);

Expand All @@ -26,8 +30,6 @@ describe('DeepLinking', () => {
});

test('removeRoute', () => {
expect(DeepLinking.routes).toEqual([]);

DeepLinking.addRoute(articleRoute.expression, articleRoute.callback);
DeepLinking.addRoute(musicRoute.expression, musicRoute.callback);
expect(DeepLinking.routes).toEqual([articleRoute, musicRoute]);
Expand All @@ -37,20 +39,38 @@ describe('DeepLinking', () => {
});

test('resetRoutes', () => {
expect(DeepLinking.routes).toEqual([]);

DeepLinking.addRoute(articleRoute.expression, articleRoute.callback);
DeepLinking.addRoute(musicRoute.expression, musicRoute.callback);
expect(DeepLinking.routes).toEqual([articleRoute, musicRoute]);

DeepLinking.resetRoutes();
expect(DeepLinking.routes).toEqual([]);
});

test('handleUrl supported url', () => {
jest.resetModules();
jest.mock('Linking', () => ({
canOpenURL: jest.fn(() => Promise.resolve(true)),
}));

return DeepLinking.handleUrl({ url: '' }).then(supported => {
expect(supported).toEqual(true);
});
});

test('handleUrl unsupported url', () => {
jest.resetModules();
jest.mock('Linking', () => ({
canOpenURL: jest.fn(() => Promise.resolve(false)),
}));

return DeepLinking.handleUrl({ url: '' }).then(supported => {
expect(supported).toEqual(false);
});
});
});

describe('Routes', () => {
DeepLinking.addScheme('domain://');

afterEach(() => {
DeepLinking.resetRoutes();
});
Expand Down Expand Up @@ -157,4 +177,55 @@ describe('Routes', () => {
evaluateUrl('domain://music/123/details');
expect(urlEvaluated).toEqual(true);
});

test('invalid scheme something://music', () => {
let urlEvaluated = false;
DeepLinking.addRoute('/music', () => {
urlEvaluated = true;
});

evaluateUrl('something://music');
expect(urlEvaluated).toEqual(false);
});

test('invalid route', () => {
let urlEvaluated = false;
DeepLinking.addRoute('music', () => {
urlEvaluated = true;
});

evaluateUrl('domain://music');
expect(urlEvaluated).toEqual(false);
});

test('invalid path', () => {
let urlEvaluated = false;
DeepLinking.addRoute('/music/:id', (response) => {
urlEvaluated = true;
});

evaluateUrl('domain://music/12/details');
expect(urlEvaluated).toEqual(false);
});

test('invalid query', () => {
let urlEvaluated = false;
DeepLinking.addRoute('/music/:', () => {
urlEvaluated = true;
});

evaluateUrl('domain://music/1');
expect(urlEvaluated).toEqual(false);
});

test('invalid path with regex domain://music/(.*)\/details/', () => {
let urlEvaluated = false;
const regex = /\/music\/(.*)\/details/g;
DeepLinking.addRoute(regex, (wtf) => {
urlEvaluated = true;
});

evaluateUrl('domain://videos/123/details');
expect(urlEvaluated).toEqual(false);
});
});

0 comments on commit 11216b5

Please sign in to comment.