Skip to content

Commit

Permalink
fixup! CHE-1687 fix sorting rule for recent workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksii Orel committed Feb 28, 2017
1 parent 88d1cd8 commit d083faa
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ describe('NavbarRecentWorkspacesController', () => {
});

workspaces = [];
for (let i = 0; i < 10; ++i) {
for (let i = 0; i < 20; ++i) {
let wrkspId = 'workspaceId' + i;
let wrkspName = 'testName' + i;
let wrkspCreateDate = new Date(2015, 1, i).toString();
let wrkspAttr = {'created': Date.parse(wrkspCreateDate)};
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);
}
Expand Down Expand Up @@ -96,39 +97,41 @@ describe('NavbarRecentWorkspacesController', () => {
});

/**
* Check recent workspaces
* 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(5);
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.created - workspace1.attributes.created;
return workspace2.attributes.updated - workspace1.attributes.updated;
});
let veryRecentWorkspaceId = testWorkspaces[testWorkspaces.length - 1].id;
testWorkspaces.splice(recentWorkspaces.length, testWorkspaces.length);

// 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);

// set 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);
})
Expand Down
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 Down Expand Up @@ -167,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(MAX_RECENT_WORKSPACES_ITEMS, recentWorkspaces.length);
recentWorkspaces[MAX_RECENT_WORKSPACES_ITEMS - 1] = veryRecentWorkspace;
}
}

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

/**
Expand Down Expand Up @@ -268,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
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

0 comments on commit d083faa

Please sign in to comment.