Is this refactor warranted?
Before:
const ROLES = ['boss', 'big boss', 'major boss', 'super boss', 'the boss'];
describe('with Alpha Team details but', () => {
describe('without Big Boss', () => {
const expected_roles = ROLES.filter(role => role !== 'big boss');
beforeEach(() => {
const model = SampleData.yetAnotherModel();
model.alphaTeam.big_boss = null;
fakeService.next(model);
fixture.detectChanges();
});
it('should not include Big Boss', () => {
expect(comp.getTeamCardDetails().length).toEqual(expected_roles.length);
});
});
describe('without Super Boss', () => {
const expected_roles = ROLES.filter(role => role !== 'super boss');
beforeEach(() => {
const model = SampleData.yetAnotherModel();
model.alphaTeam.super_boss = null;
fakeService.next(model);
fixture.detectChanges();
});
it('should not include Super Boss', () => {
expect(comp.getTeamCardDetails().length).toEqual(expected_roles.length);
});
});
});
After:
const ROLES = ['boss', 'big boss', 'major boss', 'super boss', 'the boss'];
describe('with Alpha Team details but', () => {
let excluded_role;
let expected_roles;
beforeEach(() => {
expected_roles = ROLES.filter(role => role !== excluded_role);
const model = SampleData.yetAnotherModel();
model.alphaTeam[excluded_role] = null;
fakeService.next(model);
fixture.detectChanges();
});
describe('without Big Boss', () => {
beforeEach(function () {
excluded_role = 'big boss';
});
it('should not include Big Boss', () => {
expect(comp.getTeamCardDetails().length).toEqual(expected_roles.length);
});
});
describe('without Super Boss', () => {
beforeEach(function () {
excluded_role = 'super boss';
});
it('should not include Super Boss', () => {
expect(comp.getTeamCardDetails().length).toEqual(expected_roles.length);
});
});
});
diff (--patience):
teamcard.spec.ts | 29 +++++++++++++++--------------
1 file changed, 15 insertions(+), 14 deletions(-)
@@ -107,14 +107,20 @@ describe('Alpha Team details component', () => {
});
describe('with Alpha Team details but', () => {
+ let excluded_role;
+ let expected_roles;
+
+ beforeEach(() => {
+ expected_roles = ROLES.filter(role => role !== excluded_role);
+ const model = SampleData.yetAnotherModel();
+ model.alphaTeam[excluded_role] = null;
+ fakeService.next(model);
+ fixture.detectChanges();
+ });
+
describe('without Big Boss', () => {
- const expected_roles = ROLES.filter(role => role !== 'big boss');
-
- beforeEach(() => {
- const model = SampleData.yetAnotherModel();
- model.alphaTeam.big_boss = null;
- fakeService.next(model);
- fixture.detectChanges();
+ beforeEach(function () {
+ excluded_role = 'big boss';
});
it('should not include Big Boss', () => {
@@ -123,13 +129,8 @@ describe('Alpha Team details component', () => {
});
describe('without Super Boss', () => {
- const expected_roles = ROLES.filter(role => role !== 'super boss');
-
- beforeEach(() => {
- const model = SampleData.yetAnotherModel();
- model.alphaTeam.super_boss = null;
- fakeService.next(model);
- fixture.detectChanges();
+ beforeEach(function () {
+ excluded_role = 'super boss';
});
it('should not include Super Boss', () => {