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

Remove project #13

Merged
merged 6 commits into from
Jun 17, 2019
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
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