Skip to content

Commit

Permalink
Merge pull request #570 from wheresrhys/easier-naming
Browse files Browse the repository at this point in the history
implement easier naming using just a string
  • Loading branch information
wheresrhys committed Jun 15, 2024
2 parents 6c55149 + a6f22b5 commit d61a734
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
9 changes: 6 additions & 3 deletions docs/_api-mocking/mock.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: '.mock(matcher, response, options)'
title: '.mock(matcher, response, optionsOrName)'
navTitle: .mock()
position: 1
versionAdded: 2.0.0
Expand Down Expand Up @@ -30,11 +30,14 @@ parameters:
- Promise
- Response
content: Response to send when a call is matched
- name: options
- name: optionsOrName
versionAdded: 2.0.0
types:
- Object
content: More options to configure matching and responding behaviour
- String
content: |
More options to configure matching and responding behaviour.
Alternatively, use this parameter to pass a string to use as a name for the route in order to make using the call inspection API easier.
content_markdown: |-
Alternatively a single parameter, `options`, an Object with `matcher`, `response` and other options defined, can be passed in.
Expand Down
16 changes: 13 additions & 3 deletions src/Route/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const isUrlMatcher = (matcher) =>

const isFunctionMatcher = (matcher) => typeof matcher === 'function';

const nameToOptions = (options) =>
typeof options === 'string' ? { name: options } : options;

class Route {
constructor(args, fetchMock) {
this.fetchMock = fetchMock;
Expand All @@ -34,8 +37,7 @@ class Route {
}

init(args) {
const [matcher, response, options = {}] = args;

const [matcher, response, nameOrOptions = {}] = args;
const routeConfig = {};

if (isUrlMatcher(matcher) || isFunctionMatcher(matcher)) {
Expand All @@ -48,7 +50,15 @@ class Route {
routeConfig.response = response;
}

Object.assign(routeConfig, options);
if (nameOrOptions) {
Object.assign(
routeConfig,
typeof nameOrOptions === 'string'
? nameToOptions(nameOrOptions)
: nameOrOptions,
);
}

Object.assign(this, routeConfig);
}

Expand Down
36 changes: 36 additions & 0 deletions test/specs/routing/naming-routes.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { afterEach, describe, expect, it, beforeAll } from 'vitest';

const { fetchMock } = testGlobals;
describe('multiple routes', () => {
let fm;
beforeAll(() => {
fm = fetchMock.createInstance();
fm.config.warnOnUnmatched = false;
});

afterEach(() => fm.restore());

it('property on first parameter', () => {
fm.mock({ url: 'http://a.com', name: 'my-name' }, 200);
fm.fetchHandler('http://a.com');
expect(fm.called('my-name')).toBe(true);
});

it('property on first parameter when only one parameter supplied', () => {
fm.mock({ name: 'my-name', url: 'http://a.com', response: 200 });
fm.fetchHandler('http://a.com');
expect(fm.called('my-name')).toBe(true);
});

it('property on third parameter', () => {
fm.mock('http://a.com', 200, { name: 'my-name' });
fm.fetchHandler('http://a.com');
expect(fm.called('my-name')).toBe(true);
});

it('string in third parameter', () => {
fm.mock('http://a.com', 200, 'my-name');
fm.fetchHandler('http://a.com');
expect(fm.called('my-name')).toBe(true);
});
});

0 comments on commit d61a734

Please sign in to comment.