Reusability is described in Protractor

I'm trying to create a few describe in a way that makes it easy to reuse other spec files .

  • Is there a better way to do this?
  • Is this really a bad way to do it this way?

Version 1 (my favorite)

export enum RULE_TYPE {
    TEN_DAY,
    PROFIT_TARGET,
    PERMITTED_TIMES,
    DAILY_LOSS_LIMIT,
    ACCOUNT_BALANCE,
}

interface Rule {
    type: RULE_TYPE
    status: string
    description: string
}

let defaultCombineRules = ():Rule[] => { return [
        {
            type: RULE_TYPE.TEN_DAY,
            status: 'pending',
            description: 'Trade a minimum of 10 days'
        },
        {
            type: RULE_TYPE.PROFIT_TARGET,
            status: 'pending',
            description: 'Reach profit target'
        },
        {
            type: RULE_TYPE.PERMITTED_TIMES,
            status: 'pending',
            description: 'Only trade during permitted times'
        },
        {
            type: RULE_TYPE.DAILY_LOSS_LIMIT,
            status: 'pending',
            description: 'Do not hit or exceed Daily Loss Limit'
        },
        {
            type: RULE_TYPE.ACCOUNT_BALANCE,
            status: 'pending',
            description: 'Do not allow your Account Balance to go below Trailing Max Drawdown'
        }
    ]
};

let testRules = function (rules: Rule[]) {
    describe('Rules', function () {
        let tradeReportModule: TradeReportModule;

        beforeAll(() => {
            tradeReportModule = new TradeReportModule();
        });


        var position;
        for (position in rules) {
            let rule = rules[position];
            it('should have proper ' + RULE_TYPE[rule.type], () => {
                expect(tradeReportModule.action.getMappedRules()).toContain({
                    status: rule.status,
                    description: rule.description
                });
            });
        }
    });
};

describe('Dashboard', () => {
	testRules(defaultCombineRules());
};
      

Run codeHide result


Version 2

export const customMatchers: CustomMatcherFactories = {
  toHaveRules: () => {
    return {
      compare: (rulesOnPage, testRules) => {
        let result = {
          pass: true,
          message: ''
        };
        let errors = [];
        let rule;

        for (rule in testRules) {
          if (!containsObject(testRules[rule], rulesOnPage)) {
            result['pass'] = false;
            errors.push(JSON.stringify(testRules[rule]));
          }
        }

        let error;
        for (error in errors) {
          result['message'] += "Expected rules to contain: '" + errors[error];

        }

        return result;
      }
    }
  }
};


beforeAll(() => {
  jasmine.addMatchers(customMatchers);
});

it('should have proper rules', () => {
  expect(tradeReportModule.action.getMappedRules()).toHaveRules([{
      status: 'pending',
      description: 'Trade minimum of 15 days'
    },
    {
      status: 'pending',
      description: 'Do not hit $6,000 Daily Loss Limit'
    }
  ]);
});
      

Run codeHide result


Version 3

//  A bit of pseudo code 
const expectRules1 = (obj) => {
  expect(tradeReportModule.action.getMappedRules()).toContain({
    status: 'pending',
    description: 'Trade a minimum of 4 days'
  });
};

const expectRules2 = (obj) => {
  expect(tradeReportModule.action.getMappedRules()).toContain({
    status: 'pending',
    description: 'Reach profit target'
  });
};

const expectCorrectTradeReport = (obj) => {
  expectRules1(obj);
  expectRules2(obj);
};

const expecSomthingDifferent = (obj) => {
  expectRules2(obj);
  expectRules3(obj);
};

it('should have proper rules', () => expectCorrectTradeReport(someObjectCreatedInBeforeEach));
      

Run codeHide result


+3


source to share





All Articles