-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 #1151 from eclipse/CHE-1087
CHE-1087: [dashboard] improve workspace name exists error message
- Loading branch information
Showing
7 changed files
with
202 additions
and
1 deletion.
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
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
64 changes: 64 additions & 0 deletions
64
dashboard/src/components/validator/unique-workspace-name-validator.directive.js
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,64 @@ | ||
/* | ||
* Copyright (c) 2015-2016 Codenvy, S.A. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Codenvy, S.A. - initial API and implementation | ||
*/ | ||
'use strict'; | ||
|
||
/** | ||
* Defines a directive for checking if the workspace name is not already taken | ||
* @author Oleksii Kurinnyi | ||
*/ | ||
export class UniqueWorkspaceNameValidator { | ||
|
||
/** | ||
* Default constructor that is using resource | ||
* @ngInject for Dependency injection | ||
*/ | ||
constructor (cheAPI, $q) { | ||
this.cheAPI = cheAPI; | ||
this.$q = $q; | ||
this.restrict='A'; | ||
this.require = 'ngModel'; | ||
} | ||
|
||
/** | ||
* Check that the name of workspace is unique | ||
*/ | ||
link($scope, element, attributes, ngModel) { | ||
|
||
// validate only input element | ||
if ('input' === element[0].localName) { | ||
|
||
ngModel.$asyncValidators.uniqueWorkspaceName = (modelValue) => { | ||
|
||
// create promise | ||
var deferred = this.$q.defer(); | ||
|
||
let workspaces = this.cheAPI.getWorkspace().getWorkspaces(); | ||
if (workspaces.length) { | ||
|
||
for (let i=0; i<workspaces.length; i++) { | ||
if (workspaces[i].config.name === modelValue) { | ||
deferred.reject(false); | ||
} | ||
} | ||
deferred.resolve(true); | ||
} else { | ||
// no workspaces so it's ok | ||
deferred.resolve(true); | ||
} | ||
|
||
// return promise | ||
return deferred.promise; | ||
}; | ||
} | ||
} | ||
|
||
|
||
} |
127 changes: 127 additions & 0 deletions
127
dashboard/src/components/validator/unique-workspace-name-validator.spec.js
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,127 @@ | ||
/* | ||
* Copyright (c) 2015-2016 Codenvy, S.A. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Codenvy, S.A. - initial API and implementation | ||
*/ | ||
'use strict'; | ||
|
||
/** | ||
* Test the workspace name uniqueness | ||
* @author Oleksii Kurinnyi | ||
*/ | ||
|
||
describe('unique-workspace-name-validator', function() { | ||
var $scope, form, $compiler; | ||
|
||
/** | ||
* Workspace API | ||
*/ | ||
var factoryWorkspace; | ||
|
||
/** | ||
* API builder. | ||
*/ | ||
var apiBuilder; | ||
|
||
/** | ||
* Backend for handling http operations | ||
*/ | ||
var httpBackend; | ||
|
||
/** | ||
* Che backend | ||
*/ | ||
var cheBackend; | ||
|
||
|
||
beforeEach(angular.mock.module('userDashboard')); | ||
|
||
|
||
beforeEach(inject(function($compile, $rootScope, cheWorkspace, cheAPIBuilder, cheHttpBackend, $document) { | ||
$scope = $rootScope; | ||
$compiler = $compile; | ||
factoryWorkspace = cheWorkspace; | ||
apiBuilder = cheAPIBuilder; | ||
cheBackend = cheHttpBackend; | ||
httpBackend = cheHttpBackend.getHttpBackend(); | ||
this.$document = $document; | ||
|
||
})); | ||
|
||
describe('Validate Workspace Name', function() { | ||
|
||
it('workspaceAlready exists', function() { | ||
|
||
// setup tests objects | ||
var idWorkspace1 = 'idOfMyWorkspace1'; | ||
var nameWorkspace1 = 'testWorkspace1'; | ||
var workspace1 = apiBuilder.getWorkspaceBuilder().withName(nameWorkspace1).withId(idWorkspace1).build(); | ||
|
||
factoryWorkspace.fetchWorkspaces(); | ||
|
||
// add into backend | ||
cheBackend.addWorkspaces([workspace1]); | ||
cheBackend.setup(); | ||
|
||
// flush HTTP backend | ||
httpBackend.flush(); | ||
|
||
$scope.model = { workspaceName: null}; | ||
|
||
var element = angular.element( | ||
'<form name="form">' + | ||
'<input ng-model="model.workspaceName" name="name" unique-workspace-name="model.workspaceName" />' + | ||
'</form>' | ||
); | ||
$compiler(element)($scope); | ||
form = $scope.form; | ||
|
||
form.name.$setViewValue(nameWorkspace1); | ||
|
||
// check form (expect invalid) | ||
expect(form.name.$invalid).toBe(true); | ||
expect(form.name.$valid).toBe(false); | ||
}); | ||
|
||
it('workspace not yet defined', function() { | ||
|
||
// setup tests objects | ||
var idWorkspace1 = 'idOfMyWorkspace1'; | ||
var nameWorkspace1 = 'testWorkspace1'; | ||
var workspace1 = apiBuilder.getWorkspaceBuilder().withName(nameWorkspace1).withId(idWorkspace1).build(); | ||
|
||
factoryWorkspace.fetchWorkspaces(); | ||
|
||
// add into backend | ||
cheBackend.addWorkspaces([workspace1]); | ||
|
||
// setup backend | ||
cheBackend.setup(); | ||
|
||
// flush HTTP backend | ||
httpBackend.flush(); | ||
|
||
$scope.model = { workspaceName: null }; | ||
|
||
var element = angular.element( | ||
'<form name="form">' + | ||
'<input ng-model="model.workspaceName" name="name" unique-workspace-name="model.workspaceName" />' + | ||
'</form>' | ||
); | ||
$compiler(element)($scope); | ||
form = $scope.form; | ||
|
||
form.name.$setViewValue('dummyWorkspace'); | ||
|
||
// check form valid | ||
expect(form.name.$invalid).toBe(false); | ||
expect(form.name.$valid).toBe(true); | ||
|
||
}); | ||
}); | ||
}); |
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