Skip to content

Commit

Permalink
fix: Section inheritance (#351)
Browse files Browse the repository at this point in the history
* Proxy the newly wrapped Section to maintain inheritance (#349)

* Use trapped constructor to retain inheritance

* Modernize wrapped Section

* Fix compile error. Add inheritance test

* Fix codestyle

* Add a test for Section inheritance (#350)
  • Loading branch information
Igor Muchychka committed Feb 18, 2018
1 parent d319afe commit 49a22d4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
8 changes: 5 additions & 3 deletions lib/index.js
Expand Up @@ -43,6 +43,8 @@ function getClientProxy (subPages) {

module.exports.client = getClientProxy([])

module.exports.Section = function () {
return runner.nightwatchApi.Section.apply(this, arguments)
}
module.exports.Section = new Proxy(function () {}, {
construct (target, args) {
return new runner.nightwatchApi.Section(...args)
}
})
10 changes: 5 additions & 5 deletions lib/nightwatch-api.js
@@ -1,4 +1,3 @@
const util = require('util')
const co = require('co')
const pify = require('pify')
const fs = pify(require('fs'), { include: ['readFile'] })
Expand All @@ -25,11 +24,12 @@ module.exports = class NightwatchApi {
this.options = options
this.colorsEnabled = colorsEnabled
const self = this
this.Section = function () {
Nightwatch.Section.apply(this, arguments)
self.promisifySection(this)
this.Section = class PromisedSection extends Nightwatch.Section {
constructor () {
super(...arguments)
self.promisifySection(this)
}
}
util.inherits(this.Section, Nightwatch.Section)
}

_startSession (options) {
Expand Down
39 changes: 39 additions & 0 deletions test/page-objects.test.js
Expand Up @@ -218,4 +218,43 @@ describe('Assertion features', () => {
result.features[0].scenarios[0].result.stepCounts.should.deep.equal({passed: 3})
})
})

it('should export a section that inherits correctly', () => {
return testCaseFactory
.create('section-interface-test')
.pageObject('calculator', `
const { Section } = require('../../../lib/index')
const commands = {
getDynamicSection() {
return new Section({
name: 'Dynamic Section',
parent: this,
selector: 'body'
})
}
}
module.exports = {
url: 'http://yahoo.com',
elements: {
body: 'body',
searchBar: 'input[name="p"]'
},
commands: [commands]
}`)
.feature('Section')
.scenario('toString')
.prependStepDefinition(`
const calculator = client.page.calculator();
const dynamicSection = calculator.getDynamicSection();
`)
.then('toString works', () => dynamicSection.assert.equal(dynamicSection.toString(), 'Section[name=Dynamic Section]'))
.then('parent is set', () => dynamicSection.assert.equal(dynamicSection.parent, calculator))
.run()
.then((result) => {
result.features[0].result.status.should.be.passed
result.features[0].result.scenarioCounts.should.deep.equal({passed: 1})
result.features[0].scenarios[0].result.status.should.be.passed
result.features[0].scenarios[0].result.stepCounts.should.deep.equal({passed: 2})
})
})
})

0 comments on commit 49a22d4

Please sign in to comment.