Skip to content
Permalink
Browse files
feat(interactions): new "Patch" interaction plus the CallAnApi abilit…
…y returns axios responses so th

affects: @serenity-js/rest
  • Loading branch information
jan-molak committed Jan 9, 2018
1 parent 0534bb9 commit 747580b7167712333afea890ac3ba5072d1a64b2
@@ -0,0 +1,34 @@
import sinon = require('sinon');
import 'mocha';
import { CallAnApi } from '../../../src/screenplay/abilities';
import { Patch } from '../../../src/screenplay/interactions';

import { Actor } from '@serenity-js/core/lib/screenplay';

import { expect } from '../../expect';

describe('Interactions', () => {
const baseUrl = 'https://dum.my';

describe('Patch resource', () => {
let spyAs, spyPatch;
const callAnApi = CallAnApi.at(baseUrl);
const actor = Actor.named('James').whoCan(callAnApi),
resource = '/patch';
const item = {username: '1337'};

before(() => {
spyAs = sinon.spy(CallAnApi, 'as');
spyPatch = sinon.spy(callAnApi, 'patch');
});

beforeEach(() => expect(Patch.resource(resource).with(item).performAs(actor)).to.be.eventually.fulfilled);
it('should perform as the actor.', () => expect(spyAs).to.have.been.calledOnce);
it('should perform a Patch on the resource', () => expect(spyPatch).to.have.been.calledWith(resource, item));

after(() => {
spyAs.restore();
spyPatch.restore();
});
});
});
@@ -1,5 +1,5 @@
import {Ability, UsesAbilities} from '@serenity-js/core/lib/screenplay';
import axios, {AxiosInstance, AxiosRequestConfig, AxiosResponse} from 'axios';
import { Ability, UsesAbilities } from '@serenity-js/core/lib/screenplay';
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';

/**
* Call an Api is the ability to correspond with an Api by the npm package axios.
@@ -9,7 +9,7 @@ export class CallAnApi implements Ability {
private lastResponse: AxiosResponse;

/**
* ability to Call and api at a specific baseUrl
* Ability to Call and api at a specified baseUrl
* Timeout is set to 1s and headers Accept application/json and application/xml
*
* @param {string} baseURL
@@ -19,7 +19,7 @@ export class CallAnApi implements Ability {
const axiosInstance: AxiosInstance = axios.create({
baseURL,
timeout: 2000,
headers: {Accept: 'application/json,application/xml'},
headers: { Accept: 'application/json,application/xml' },
});
return new CallAnApi(axiosInstance);
}
@@ -45,64 +45,80 @@ export class CallAnApi implements Ability {
}

/**
* Call the api method get on the url.
* Every response will be resolved and put into the lastResponse.
* Send a GET request to a specified url.
* Response will be resolved and made available as lastResponse.
*
* @param {string} url
* @param {AxiosRequestConfig} config
* @returns {PromiseLike<void>}
*/
get(url: string, config?: AxiosRequestConfig): PromiseLike<void> {
get(url: string, config?: AxiosRequestConfig): PromiseLike<AxiosResponse> {
return this.axiosInstance.get(url, config).then(
fulfilled => Promise.resolve(this.lastResponse = fulfilled),
rejected => Promise.resolve(this.lastResponse = rejected.response),
fulfilled => this.lastResponse = fulfilled,
rejected => this.lastResponse = rejected.response,
);
}

/**
* Call the api method post on the url.
* Every response will be resolved and put into the lastResponse.
* Response will be resolved and made available as lastResponse.
*
* @param {string} url
* @param data
* @param {AxiosRequestConfig} config
* @returns {PromiseLike<void>}
*/
post(url: string, data?: any, config?: AxiosRequestConfig): PromiseLike<void> {
post(url: string, data?: any, config?: AxiosRequestConfig): PromiseLike<AxiosResponse> {
return this.axiosInstance.post(url, data, config).then(
fulfilled => Promise.resolve(this.lastResponse = fulfilled),
rejected => Promise.resolve(this.lastResponse = rejected.response),
fulfilled => this.lastResponse = fulfilled,
rejected => this.lastResponse = rejected.response,
);
}

/**
* Call the api method delete on the url.
* Send a DELETE request to a specified url.
* Every response will be resolved and put into the lastResponse.
*
* @param {string} url
* @param {AxiosRequestConfig} config
* @returns {PromiseLike<void>}
*/
delete(url: string, config?: AxiosRequestConfig): PromiseLike<void> {
delete(url: string, config?: AxiosRequestConfig): PromiseLike<AxiosResponse> {
return this.axiosInstance.delete(url, config).then(
fulfilled => Promise.resolve(this.lastResponse = fulfilled),
rejected => Promise.resolve(this.lastResponse = rejected.response),
fulfilled => this.lastResponse = fulfilled,
rejected => this.lastResponse = rejected.response,
);
}

/**
* Call the api method put on the url.
* Every response will be resolved and put into the lastResponse.
* PATCH the resource at a specified url.
* Response will be resolved and made available as lastResponse.
*
* @param {string} url
* @param data
* @param {AxiosRequestConfig} config
* @returns {PromiseLike<void>}
*/
patch(url: string, data?: any, config?: AxiosRequestConfig): PromiseLike<AxiosResponse> {
return this.axiosInstance.patch(url, data, config).then(
fulfilled => this.lastResponse = fulfilled,
rejected => this.lastResponse = rejected.response,
);
}

/**
* Send a PUT request to a specified url.
* Response will be resolved and made available as lastResponse.
*
* @param {string} url
* @param data
* @param {AxiosRequestConfig} config
* @returns {PromiseLike<void>}
*/
put(url: string, data?: any, config?: AxiosRequestConfig): PromiseLike<void> {
put(url: string, data?: any, config?: AxiosRequestConfig): PromiseLike<AxiosResponse> {
return this.axiosInstance.put(url, data, config).then(
fulfilled => Promise.resolve(this.lastResponse = fulfilled),
rejected => Promise.resolve(this.lastResponse = rejected.response),
fulfilled => this.lastResponse = fulfilled,
rejected => this.lastResponse = rejected.response,
);
}

@@ -12,5 +12,5 @@ export class Delete implements Interaction {
constructor(private resource: string) {
}

toString = () => `{0} execute a DELETE on resource ${this.resource}`;
toString = () => `#actor executes a DELETE on resource ${this.resource}`;
}
@@ -11,5 +11,5 @@ export class Get implements Interaction {

constructor(private resource: string) { }

toString = () => `{0} execute a GET on resource ${this.resource}`;
toString = () => `#actor executes a GET on resource ${this.resource}`;
}
@@ -1,4 +1,5 @@
export * from './delete';
export * from './get';
export * from './patch';
export * from './post';
export * from './put';
@@ -0,0 +1,16 @@
import { Interaction, UsesAbilities } from '@serenity-js/core/lib/screenplay';
import { CallAnApi } from '../abilities';

export class Patch implements Interaction {

static resource = (resource: string) => ({ with: (item: any) => new Patch(item, resource) });

performAs(actor: UsesAbilities): PromiseLike<any> {
return CallAnApi.as(actor).patch(this.resource, this.item);
}

constructor(private item: any, private resource: string) {
}

toString = () => `#actor executes a PATCH on resource ${this.resource} with item: ${this.item}`;
}
@@ -12,5 +12,5 @@ export class Post implements Interaction {
constructor(private item: any, private resource: string) {
}

toString = () => `{0} execute a POST on resource ${this.resource} with item: ${this.item}`;
toString = () => `#actor executes a POST on resource ${this.resource} with item: ${this.item}`;
}
@@ -12,5 +12,5 @@ export class Put implements Interaction {
constructor(private item: any, private resource: string) {
}

toString = () => `{0} execute a PUT on resource ${this.resource} with item: ${this.item}`;
toString = () => `#actor executes a PUT on resource ${this.resource} with item: ${this.item}`;
}

0 comments on commit 747580b

Please sign in to comment.