Skip to content

Commit

Permalink
Add spy.hasProps
Browse files Browse the repository at this point in the history
  • Loading branch information
vikingair committed Mar 30, 2022
1 parent 98a7beb commit 2cf99a4
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [3.4.0] - 2022-03-30
### Added
- `spy.hasProps` to check for current props.

## [3.3.2] - 2022-03-21
### Fixed
- Generic React mocks were causing issues due to illegal named properties.
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ Even as important are the "facts", we want to display:
- `wasCalledWith` (does display that the spy has been called at least once like with the provided params)
- `wasNotCalledWith` (does display that the spy was never like with the provided params)
- `hasCallHistory` (does display that the spy has been called with the following params in the given order)
- `hasProps` (does display that the spy has been called with the given argument on the last invocation. In the
context of React: The spy being a mocked React component has currently the given props.)

Those methods on a spy display facts. Facts have to be true, otherwise they
will throw an Exception, which displays in a formatted debug message why the
Expand Down Expand Up @@ -502,6 +504,14 @@ ATTENTION: single argument calls can be provided without wrapping into an array.
the single argument is an array itself, then you have to warp it also yourself. (Inspired by jest
data providers)
### hasProps (fact)
```ts
spy.hasProps(props: any) => void
```
This fact displays that the spy has currently the given props in React context of a mocked component.
This is the corresponding fact for [getProps](#getprops) using the spy diffing.
### getAllCallArguments
```ts
spy.getAllCallArguments() => Array<any[]>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "spy4js",
"version": "3.3.2",
"version": "3.4.0",
"description": "Smart, compact and powerful spy test framework",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
Expand Down
31 changes: 23 additions & 8 deletions src/spy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export type SpyInstance = {
getCallArgument: (callNr?: number, argNr?: number) => any;
getLatestCallArgument: (argNr?: number) => any;
getProps: () => any;
hasProps: (props: any) => void;
getCallCount: () => number;
showCallArguments: (additionalInformation?: (string | undefined)[]) => string;

Expand Down Expand Up @@ -496,14 +497,6 @@ const SpyFunctions = {
*
* It throws an error if the upper method would not.
*
* For example:
* const spy = Spy();
* spy(arg1, arg2, arg3);
* spy(arg4, arg5);
* spy.wasCalledWith(arg1); // no error
* spy.wasCalledWith(arg4, arg3); // no error
* spy.wasCalledWith(arg4, arg5); // error!!!
*
* @param {Array<any>} args -> The not expected arguments
* for any made call.
*/
Expand Down Expand Up @@ -627,6 +620,28 @@ const SpyFunctions = {
return this.getLatestCallArgument();
},

/**
* Checks if the spy was last call was done with the provided first argument.
*
* It throws an error otherwise.
*
* @param {any} props
*
*/
hasProps(this: SpyInstance, props: any) {
const currentProps = this.getProps();
const diff = differenceOf(currentProps, props, this[Symbols.config]);
if (!diff) return;
throw new Error(
`\n\n${this[Symbols.name]} was expected` +
' to have the following props:\n\n' +
` --> ${serialize(props)}\n\n` +
'But the current props are:\n\n' +
` ${serialize(currentProps)}\n` +
` ${diff}`
);
},

/**
* This method returns the number of made calls on the spy.
*
Expand Down
6 changes: 6 additions & 0 deletions test/spy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,16 +213,21 @@ describe('Spy - Utils', () => {
const testArg2 = { _key: 'test2' };
const testArg3 = { _key: 'test3' };
expect(() => spy.getLatestCallArgument()).toThrow(/spy was never called/);
expect(() => spy.hasProps(testArg1)).toThrow(/spy was never called/);
spy(testArg1);
expect(spy.getProps()).toBe(testArg1);
spy.hasProps(testArg1);
expect(() => spy.hasProps(testArg2)).toThrow(/But the current props are:\s+{_key: 'test1'}/);
spy(testArg1, testArg2);
expect(spy.getProps()).toBe(testArg1);
expect(spy.getLatestCallArgument(1)).toBe(testArg2);
spy(testArg3, testArg2, testArg1);
expect(spy.getProps()).toBe(testArg3);
spy.hasProps(testArg3);
expect(spy.getLatestCallArgument(2)).toBe(testArg1);
spy(testArg2);
expect(spy.getProps()).toBe(testArg2);
spy.hasProps(testArg2);

expect(spy.getCallArguments()).toEqual([testArg1]);
expect(spy.getCallArguments(0)).toEqual([testArg1]);
Expand Down Expand Up @@ -773,6 +778,7 @@ describe('Spy - Utils', () => {
'getCallArgument',
'getLatestCallArgument',
'getProps',
'hasProps',
'getCallCount',
'showCallArguments',
];
Expand Down

0 comments on commit 2cf99a4

Please sign in to comment.