Skip to content

Instantly share code, notes, and snippets.

@rctay
Last active April 6, 2018 06:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rctay/54a54b1ac11959022cc18becc5c1181e to your computer and use it in GitHub Desktop.
Save rctay/54a54b1ac11959022cc18becc5c1181e to your computer and use it in GitHub Desktop.
soliloquy 20180406

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', () => {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment