Skip to content

Commit

Permalink
CHE-1687 fix sorting rule for recent workspaces
Browse files Browse the repository at this point in the history
Signed-off-by: Oleksii Orel <oorel@codenvy.com>
  • Loading branch information
Oleksii Orel committed Mar 1, 2017
1 parent 94b9418 commit 51b6abe
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 22 deletions.
139 changes: 139 additions & 0 deletions src/app/navbar/recent-workspaces/recent-workspaces.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* Copyright (c) 2015-2017 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';
import {CheWorkspace} from '../../../components/api/che-workspace.factory';
import {CheAPIBuilder} from '../../../components/api/builder/che-api-builder.factory';
import {CheHttpBackend} from '../../../components/api/test/che-http-backend';
import IdeSvc from '../../ide/ide.service';


/**
* Test of the NavbarRecentWorkspacesController
*/
describe('NavbarRecentWorkspacesController', () => {
/**
* NavbarRecentWorkspacesController
*/
let navbarRecentWorkspacesController;

/**
* API builder
*/
let apiBuilder: CheAPIBuilder;

/**
* Backend for handling http operations
*/
let httpBackend: ng.IHttpBackendService;

/**
* Che backend
*/
let cheBackend: CheHttpBackend;


let workspaces: Array<che.IWorkspace>;

/**
* setup module
*/
beforeEach(angular.mock.module('userDashboard'));

/**
* Inject factory and http backend
*/
beforeEach(inject(($rootScope: ng.IRootScopeService, cheWorkspace: CheWorkspace, cheAPIBuilder: CheAPIBuilder, cheHttpBackend: CheHttpBackend, $controller: any, ideSvc: IdeSvc, $window: ng.IWindowService, $log: ng.ILogService) => {
apiBuilder = cheAPIBuilder;
cheBackend = cheHttpBackend;
httpBackend = cheHttpBackend.getHttpBackend();

let scope = $rootScope.$new();
navbarRecentWorkspacesController = $controller('NavbarRecentWorkspacesController', {
ideSvc: IdeSvc, cheWorkspace: cheWorkspace, $window: $window, $log: $log, $scope: scope, $rootScope: $rootScope
});

workspaces = [];
for (let i = 0; i < 20; ++i) {
let wrkspId = 'workspaceId' + i;
let wrkspName = 'testName' + i;
let wrkspCreateDate = new Date(2001, 1, 1, i, 1).toString();
let wrkspUpdateDate = new Date(2001, 1, 1, i, 2).toString();
let wrkspAttr = {'created': Date.parse(wrkspCreateDate), 'updated': Date.parse(wrkspUpdateDate)};
let workspace = apiBuilder.getWorkspaceBuilder().withId(wrkspId).withAttributes(wrkspAttr).withName(wrkspName).build();
workspaces.push(workspace);
}
// shuffle the workspaces
workspaces.sort(() => {
return 0.5 - Math.random();
});
// providing request
// add workspaces on Http backend
cheBackend.addWorkspaces(workspaces);

// setup backend
cheBackend.setup();

// fetch workspaces
cheWorkspace.fetchWorkspaces();

// flush command
httpBackend.flush();
}));

/**
* Check assertion after the test
*/
afterEach(() => {
httpBackend.verifyNoOutstandingExpectation();
httpBackend.verifyNoOutstandingRequest();
});

/**
* Check sorting rule for recent workspaces
*/
it('Check very recent workspaces', inject(() => {
// get recentWorkspaces
let recentWorkspaces = navbarRecentWorkspacesController.getRecentWorkspaces();

// check max length
expect(recentWorkspaces.length).toEqual(navbarRecentWorkspacesController.maxItemsNumber);

// prepare test objects
let testWorkspaces: Array<che.IWorkspace> = angular.copy(workspaces);
testWorkspaces.sort((workspace1: che.IWorkspace, workspace2: che.IWorkspace) => {
return workspace2.attributes.updated - workspace1.attributes.updated;
});
let veryRecentWorkspaceId = testWorkspaces[testWorkspaces.length - 1].id;

// check default sorting
let lastPosition = recentWorkspaces.length - 1;
for (let i = 0; i < lastPosition; i++) {
expect(recentWorkspaces[i].id).toEqual(testWorkspaces[i].id);
}
// check the last one workspace is equal to the last test workspace and not equal to the very recent workspace,
// because we are going to update very recent workspace in controller and sorting rule should be changed
expect(recentWorkspaces[lastPosition].id).toEqual(testWorkspaces[lastPosition].id);
expect(recentWorkspaces[lastPosition].id).not.toEqual(veryRecentWorkspaceId);

// update very recent workspace
navbarRecentWorkspacesController.updateRecentWorkspace(veryRecentWorkspaceId);
recentWorkspaces = navbarRecentWorkspacesController.getRecentWorkspaces();

// check sorting with veryRecentWorkspace
for (let i = 0; i < lastPosition; i++) {
expect(recentWorkspaces[i].id).toEqual(testWorkspaces[i].id);
}
// check the last one workspace is equal to the very recent workspace and not equal to the last test workspace
expect(recentWorkspaces[lastPosition].id).not.toEqual(testWorkspaces[lastPosition].id);
expect(recentWorkspaces[lastPosition].id).toEqual(veryRecentWorkspaceId);
})
);
});
20 changes: 11 additions & 9 deletions src/app/navbar/recent-workspaces/recent-workspaces.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ export class NavbarRecentWorkspacesController {
this.fetchWorkspaceSettings();
}

/**
* Returns the MAX_RECENT_WORKSPACES_ITEMS constant
* @returns {number}
*/
get maxItemsNumber(): number {
return MAX_RECENT_WORKSPACES_ITEMS;
}

/**
* Retrieves workspace settings.
*/
Expand All @@ -84,10 +92,6 @@ export class NavbarRecentWorkspacesController {
} else {
this.cheWorkspace.fetchWorkspaceSettings().then(() => {
this.prepareDropdownItemsTemplate();
}, (error: any) => {
if (error.status === 304) {
this.prepareDropdownItemsTemplate();
}
});
}
}
Expand Down Expand Up @@ -171,13 +175,10 @@ export class NavbarRecentWorkspacesController {
if (recentWorkspaces.length > MAX_RECENT_WORKSPACES_ITEMS) {
let pos: number = veryRecentWorkspace ? recentWorkspaces.indexOf(veryRecentWorkspace) : -1;
if (veryRecentWorkspace && pos >= MAX_RECENT_WORKSPACES_ITEMS) {
recentWorkspaces.splice(MAX_RECENT_WORKSPACES_ITEMS - 1, recentWorkspaces.length , veryRecentWorkspace);
} else {
recentWorkspaces.splice(0, MAX_RECENT_WORKSPACES_ITEMS);
recentWorkspaces[MAX_RECENT_WORKSPACES_ITEMS - 1] = veryRecentWorkspace;
}
}

this.recentWorkspaces = recentWorkspaces;
this.recentWorkspaces = recentWorkspaces.slice(0, MAX_RECENT_WORKSPACES_ITEMS);
}

/**
Expand Down Expand Up @@ -272,6 +273,7 @@ export class NavbarRecentWorkspacesController {
*/
stopRecentWorkspace(workspaceId: string, createSnapshot: boolean): void {
this.cheWorkspace.stopWorkspace(workspaceId, createSnapshot).then(() => {
angular.noop();
}, (error: any) => {
this.$log.error(error);
});
Expand Down
2 changes: 1 addition & 1 deletion src/app/navbar/recent-workspaces/recent-workspaces.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</md-button>
</md-list-item>
<md-list-item flex class="navbar-subsection-item"
ng-repeat="workspace in navbarRecentWorkspacesController.getRecentWorkspaces()"
ng-repeat="workspace in navbarRecentWorkspacesController.getRecentWorkspaces() | limitTo: navbarRecentWorkspacesController.maxItemsNumber"
ng-class="{'recent-workspaces-last-opened': navbarRecentWorkspacesController.isOpen(workspace.id)}">
<navbar-dropdown-menu flex
navbar-dropdown-items="navbarRecentWorkspacesController.getDropdownItems(workspace.id)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ export class WorkspaceEnvironmentsController {
*/
getLocationUrl(): string {
let url: string = '';
if (this.environment && this.environment.recipe.location && /^https?:\/\//m.test(this.environment.recipe.location)) {
if (this.environment && this.environment.recipe.location && /^https?:\/\//i.test(this.environment.recipe.location)) {
url = this.environment.recipe.location;
}
return url;
Expand Down
21 changes: 16 additions & 5 deletions src/components/api/builder/che-workspace-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
*/
'use strict';

export interface IWorkspaceAttributes {
created: number;
updated?: number;
[propName: string]: string | number;
}

/**
* This class is providing a builder for Workspace
Expand All @@ -20,6 +25,7 @@ export class CheWorkspaceBuilder {

constructor() {
this.workspace = {
name: 'test',
temporary: false,
config: {
projects: []
Expand All @@ -28,27 +34,32 @@ export class CheWorkspaceBuilder {

}

withName(name: string) {
withName(name: string): CheWorkspaceBuilder {
this.workspace.config.name = name;
return this;
}

withId(id: string) {
withId(id: string): CheWorkspaceBuilder {
this.workspace.id = id;
return this;
}

withTemporary(temporary: boolean) {
withAttributes(attributes: IWorkspaceAttributes): CheWorkspaceBuilder {
this.workspace.attributes = attributes;
return this;
}

withTemporary(temporary: boolean): CheWorkspaceBuilder {
this.workspace.temporary = temporary;
return this;
}

withRuntime(runtime: any) {
withRuntime(runtime: any): CheWorkspaceBuilder {
this.workspace.runtime = runtime;
return this;
}

build() {
build(): che.IWorkspace {
return this.workspace;
}

Expand Down
18 changes: 12 additions & 6 deletions src/components/api/che-workspace.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,10 @@ export class CheWorkspace {
*/
stopWorkspace(workspaceId: string, createSnapshot: boolean): ng.IPromise<any> {
createSnapshot = createSnapshot === undefined ? this.getAutoSnapshotSettings() : createSnapshot;
return this.remoteWorkspaceAPI.stopWorkspace({workspaceId: workspaceId, createSnapshot: createSnapshot}, {}).$promise;
return this.remoteWorkspaceAPI.stopWorkspace({
workspaceId: workspaceId,
createSnapshot: createSnapshot
}, {}).$promise;
}

/**
Expand All @@ -458,7 +461,6 @@ export class CheWorkspace {
*/
updateWorkspace(workspaceId: string, data: che.IWorkspace): ng.IPromise<any> {
let defer = this.$q.defer();

let promise = this.remoteWorkspaceAPI.updateWorkspace({workspaceId: workspaceId}, data).$promise;
promise.then((data: che.IWorkspace) => {
this.workspacesById.set(data.id, data);
Expand Down Expand Up @@ -609,13 +611,17 @@ export class CheWorkspace {
*
* @returns {IPromise<TResult>}
*/
fetchWorkspaceSettings(): ng.IPromise {
fetchWorkspaceSettings(): ng.IPromise<any> {
let promise = this.remoteWorkspaceAPI.getSettings().$promise;
let resultPromise = promise.then((settings: any) => {
return promise.then((settings: any) => {
this.workspaceSettings = settings;
return this.workspaceSettings;
}, (error: any) => {
if (error.status === 304) {
return this.workspaceSettings;
}
return this.$q.reject(error);
});

return resultPromise;
}

/**
Expand Down
26 changes: 26 additions & 0 deletions src/components/api/test/che-http-backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
*/
export class CheHttpBackend {

private isAutoSnapshot: boolean = false;
private isAutoRestore: boolean = false;

/**
* Constructor to use
*/
Expand Down Expand Up @@ -52,6 +55,13 @@ export class CheHttpBackend {
this.addWorkspaceAgent(key, tmpWorkspace.runtime);
this.httpBackend.when('GET', '/api/workspace/' + key).respond(tmpWorkspace);
}

let workspacSettings = {
'che.workspace.auto_snapshot': this.isAutoSnapshot,
'che.workspace.auto_restore': this.isAutoRestore
};
this.httpBackend.when('GET', '/api/workspace/settings').respond(200, workspacSettings);

this.httpBackend.when('OPTIONS', '/api/').respond({});
this.httpBackend.when('GET', '/api/workspace/settings').respond({});

Expand Down Expand Up @@ -90,6 +100,22 @@ export class CheHttpBackend {

}

/**
* Set workspace auto snapshot status
* @param isAutoSnapshot {boolean}
*/
setWorkspaceAutoSnapshot(isAutoSnapshot: boolean) {
this.isAutoSnapshot = isAutoSnapshot;
}

/**
* Set workspace auto restore status
* @param isAutoRestore {boolean}
*/
setWorkspaceAutoRestore(isAutoRestore: boolean) {
this.isAutoRestore = isAutoRestore;
}


/**
* Add the given workspaces on this backend
Expand Down

0 comments on commit 51b6abe

Please sign in to comment.