Skip to content

Commit

Permalink
Refactor getProjectById, added getActiveProject, and added removeProj…
Browse files Browse the repository at this point in the history
…ect (#13)
  • Loading branch information
JenniferMah authored and childish-sambino committed Jun 17, 2019
1 parent d538dba commit 3a44063
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/services/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,36 @@ class ConfigData {
if (!projectId) {
project = this.getProjectFromEnvironment();
}
if (!project) {
if (projectId) {
project = this.projects.find(project => project.id === projectId);
} else {
project = this.getActiveProject();
}
}
return project;
}

getActiveProject() {
let project;
if (this.projects.length > 0) {
if (this.activeProject) {
project = this.projects.find(project => project.id === this.activeProject);
}
if (!project) {
const selectedProjectId = projectId || this.activeProject;
if (selectedProjectId) {
project = this.projects.find(project => project.id === selectedProjectId);
} else {
project = this.projects[0];
}
project = this.projects[0];
}
}

return project;
}

removeProject(projectToRemove) {
this.projects = this.projects.filter(project => {
return project.id !== projectToRemove.id;
});
if (projectToRemove.id === this.activeProject) {
this.activeProject = null;
}
}

addProject(id, accountSid, region) {
Expand Down
70 changes: 70 additions & 0 deletions test/services/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,76 @@ describe('services', () => {
});
});

describe('ConfigData.getActiveProject', () => {
test.it('should return first project when no active project is set', () => {
const configData = new ConfigData();
configData.addProject('firstProject', constants.FAKE_ACCOUNT_SID);
configData.addProject('secondProject', 'new_account_SID');
configData.addProject('thirdProject', 'newest_account_SID');
const active = configData.getActiveProject();

expect(active.id).to.equal('firstProject');
expect(active.accountSid).to.equal(constants.FAKE_ACCOUNT_SID);
});
test.it('should return active project when active project has been set', () => {
const configData = new ConfigData();
configData.addProject('firstProject', constants.FAKE_ACCOUNT_SID);
configData.addProject('secondProject', 'new_account_SID');
configData.addProject('thirdProject', 'newest_account_SID');
configData.activeProject = 'secondProject';
const active = configData.getActiveProject();

expect(active.id).to.equal('secondProject');
expect(active.accountSid).to.equal('new_account_SID');
});
test.it('should return undefined if project does not exist and there are no projects configured', () => {
const configData = new ConfigData();
const active = configData.getActiveProject();
expect(active).to.equal(undefined);
});
});

describe('ConfigData.removeProject', () => {
test.it('remove a project that does not exist', () => {
const configData = new ConfigData();
configData.addProject('firstProject', constants.FAKE_ACCOUNT_SID);
configData.addProject('secondProject', 'new_account_SID');
configData.addProject('thirdProject', 'newest_account_SID');
const fakeProject = {
id: 'DOES_NOT_EXIST',
accountSid: 'fake_SID'
};
const originalLength = configData.projects.length;
configData.removeProject(fakeProject);

expect(configData.projects.length).to.equal(originalLength);
});
test.it('removes project', () => {
const configData = new ConfigData();
configData.addProject('firstProject', constants.FAKE_ACCOUNT_SID);
configData.addProject('secondProject', 'new_account_SID');
configData.addProject('thirdProject', 'newest_account_SID');
const project = configData.getProjectById('secondProject');
configData.removeProject(project);

expect(configData.projects[1].id).to.equal('thirdProject');
expect(configData.projects[1].accountSid).to.equal('newest_account_SID');
});
test.it('removes active project', () => {
const configData = new ConfigData();
configData.addProject('firstProject', constants.FAKE_ACCOUNT_SID);
configData.addProject('secondProject', 'new_account_SID');
configData.addProject('thirdProject', 'newest_account_SID');
const project = configData.getProjectById('firstProject');
configData.activeProject = 'firstProject';
configData.removeProject(project);

expect(configData.projects[1].id).to.equal('thirdProject');
expect(configData.projects[1].accountSid).to.equal('newest_account_SID');
expect(configData.activeProject).to.equal(null);
});
});

describe('Config', () => {
const tempConfigDir = tmp.dirSync({ unsafeCleanup: true });

Expand Down

0 comments on commit 3a44063

Please sign in to comment.