Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: #153 - Passing state parameters inside redirectTo #200

Merged
merged 1 commit into from
Mar 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/core/permissionModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@

return permissions
.resolveRedirectState(rejectedPermission)
.then(function (redirectStateName) {
$state.go(redirectStateName, toParams);
.then(function (redirect) {
$state.go(redirect.state, redirect.params, redirect.options);
});
})
.finally(function () {
Expand Down
25 changes: 20 additions & 5 deletions src/models/PermissionMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@
}

if (angular.isString(this.redirectTo)) {
return $q.resolve(this.redirectTo);
return $q.resolve({
state: this.redirectTo
});
}

// If redirectTo state is not defined stay where you are
Expand All @@ -70,10 +72,17 @@
return $q
.when(redirectFunction.call(null, permission))
.then(function (redirectState) {
if (!angular.isString(redirectState)) {
throw new TypeError('When used "redirectTo" as function, returned value must be string with state name');
if (angular.isString(redirectState)) {
return {
state: redirectState
};
}

if (angular.isObject(redirectState)) {
return redirectState;
}
return redirectState;

throw new TypeError('When used "redirectTo" as function, returned value must be string or object');
});
}

Expand All @@ -100,9 +109,15 @@
return resolveFunctionRedirect(redirectState, permission);
}

if (angular.isString(redirectState)) {
if (angular.isObject(redirectState)) {
return $q.resolve(redirectState);
}

if (angular.isString(redirectState)) {
return $q.resolve({
state: redirectState
});
}
}

/**
Expand Down
103 changes: 99 additions & 4 deletions test/models/PermissionMap/redirectToParam.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,46 @@ describe('model: PermissionMap', function () {
expect($state.current.name).not.toBe('default');
});

it('should redirect based on results of rejected permission with function', function () {
it('should redirect based on results of rejected permission with redirection object', function () {
// GIVEN
var sut = {
state: 'redirected',
params: {
paramOne: 'one',
paramTwo: 'two'
},
options: {
reload: true
}
};

$stateProvider
.state('redirect', {
data: {
permissions: {
only: ['denied'],
redirectTo: {
denied: sut,
default: 'default'
}
}
}
})
.state('default', {})
.state('redirected', {});

// WHEN
$state.go('redirect');
$rootScope.$apply();

// THEN
expect($state.current.name).toBe('redirected');
expect($state.current.name).not.toBe('default');
expect($state.current.params).not.toBe(sut.params);
expect($state.current.params).not.toBe(sut.options);
});

it('should redirect based on results of rejected permission with function returning string', function () {
// GIVEN
$stateProvider
.state('redirect', {
Expand Down Expand Up @@ -182,6 +221,37 @@ describe('model: PermissionMap', function () {
expect($state.current.name).not.toBe('default');
});


it('should redirect based on results of rejected permission with function returning object', function () {
// GIVEN
$stateProvider
.state('redirect', {
data: {
permissions: {
only: ['denied'],
redirectTo: {
denied: function () {
return {
state: 'redirected'
};
},
default: 'default'
}
}
}
})
.state('default', {})
.state('redirected', {});

// WHEN
$state.go('redirect');
$rootScope.$apply();

// THEN
expect($state.current.name).toBe('redirected');
expect($state.current.name).not.toBe('default');
});

it('should redirect based on results of rejected permission with promise', function () {
// GIVEN
$stateProvider
Expand Down Expand Up @@ -212,7 +282,7 @@ describe('model: PermissionMap', function () {
});

describe('used as function/promise', function () {
it('should throw error when function do not return state string', function () {
it('should throw error when function do not return state string or object', function () {
// GIVEN
var error;

Expand All @@ -238,10 +308,10 @@ describe('model: PermissionMap', function () {
}

// THEN
expect(error.message).toBe('When used "redirectTo" as function, returned value must be string with state name');
expect(error.message).toBe('When used "redirectTo" as function, returned value must be string or object');
});

it('should redirect based on results of function', function () {
it('should redirect based on string returned results of function', function () {
// GIVEN
$stateProvider
.state('redirect', {
Expand All @@ -264,6 +334,31 @@ describe('model: PermissionMap', function () {
expect($state.current.name).toBe('other');
});

it('should redirect based on object returned results of function', function () {
// GIVEN
$stateProvider
.state('redirect', {
data: {
permissions: {
only: ['denied'],
redirectTo: function () {
return {
state: 'other'
};
}
}
}
})
.state('other', {});

// WHEN
$state.go('redirect');
$rootScope.$apply();

// THEN
expect($state.current.name).toBe('other');
});

it('should redirect with promise based functions', function () {
// GIVEN
$stateProvider.state('redirect', {
Expand Down