Skip to content

Commit

Permalink
Feat(RequestMatcher): Processor now return the proxied matcher
Browse files Browse the repository at this point in the history
It allow chaining conditions without using `on` getter again
  • Loading branch information
liqueurdetoile committed Nov 7, 2019
1 parent c2ce9d9 commit 9288997
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/helpers/RequestMatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ export class RequestMatcher {
case 'username':
processor = new StringProcessor(key, this)
break;
case Symbol.toStringTag:
case 'outerHTML':
case 'tagName':
case 'nodeName':
return this[key];
default:
throw new FMFException(`Unsupported request parameter "${key}" to check`);
}
Expand Down
8 changes: 7 additions & 1 deletion src/processors/BaseProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ export default class BaseProcessor {
return this;
}

get matcher() {
return new Proxy(this._matcher, {
get: (obj, prop) => prop in obj ? obj[prop] : obj.getProcessor(prop)
});
}

async _equal(current, expected, request) {
if (expected instanceof Function) return await expected(current, this._key, request);
if (expected instanceof RegExp) return expected.test(current);
Expand All @@ -41,7 +47,7 @@ export default class BaseProcessor {
return await this._equal(current, expected, request);
}

return this._matcher;
return this.matcher;
}

equals(expected) {
Expand Down
2 changes: 1 addition & 1 deletion src/processors/BodyProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ export default class BodyProcessor extends BaseProcessor {
return await this._equal(current, expected, request);
}

return this._matcher;
return this.matcher;
}
}
2 changes: 1 addition & 1 deletion src/processors/HeadersProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ export default class HeadersProcessor extends BaseProcessor {
return await this._equal(this._name ? headers.get(this._name) : headers, expected);
}

return this._matcher;
return this.matcher;
}
}
17 changes: 17 additions & 0 deletions tests/units/RequestMatcher.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,21 @@ describe('Request configurator test suite', function() {

expect(f.on.equal.bind(f, {foo: 'bar'})).to.throw();
})

it('should chain multiple conditions on the same matcher without on keyword', function() {
const f = new Fixture();

f.on.pathname.equals(/api/).method.equals('GET').should.deep.equal(f._matcher);
})

it('should chain multiple on to generate multiple matching requests', function() {
const f = new Fixture();
const first = f.on.method.equal('GET').respond.body('GET');

first.should.be.instanceof(Fixture);

const second = first.on.method.equal('POST').respond.body('POST');

second.should.be.instanceof(Fixture);
})
})
2 changes: 1 addition & 1 deletion tests/units/fixtures.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ describe('Fixtures test suite', function() {
/**
* @test {ResponseConfigurator#pattern}
*/
describe.only('Fixture patterns', function() {
describe('Fixture patterns', function() {
it('should handle patterns', async function() {
let croot, cid;

Expand Down

0 comments on commit 9288997

Please sign in to comment.