Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DuncanMcPherson committed Dec 19, 2023
1 parent 6508ed1 commit 9f63720
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 43 deletions.
139 changes: 137 additions & 2 deletions __tests__/auto-mocker-plus.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { AutoMockerPlus, readObservableSynchronously } from "../src";
import { Observable, of } from "rxjs";
import { AutoMockerPlus, readObservableErrorSynchronously, readObservableSynchronously } from "../src";
import { forkJoin, Observable, of } from "rxjs";
import { TestEmissionsCounter } from "../src/test-emissions-counter";

class TestMockClass {
public getObservable$(): Observable<boolean> {
return of(true);
}

public getUpdatedValue(key: string): Observable<boolean> {
return key === 'false' ? of(false) : of(true);
}

public testProperty$ = of('I am a silly string');
}

describe('AutoMockerPlus', () => {
Expand Down Expand Up @@ -82,5 +88,134 @@ describe('AutoMockerPlus', () => {
expect(counter.hasActiveSubscribers).toEqual(false);
expect(counter.allSubscriptionsFinalized).toEqual(true);
});
});

describe('withReturnNonCompletingCountedObservable', () => {
it('should throw an error when method is not a spy', () => {
const fn = () => undefined;
expect(() => autoMockerPlus.withReturnNonCompletingCountedObservable(fn)).toThrow();
});

it('should return a counted subscription', (done) => {
const value = Math.floor(Math.random() * 100) > 50;
const result = autoMockerPlus.withReturnNonCompletingCountedObservable(mock.getObservable$, value);

result.countedObservable$.subscribe((res) => {
expect(res).toEqual(value);
expect(result.hasActiveSubscribers).toEqual(true);
expect(result.activeSubscriptionCount).toEqual(1);
done();
})
})
})

describe("withReturnObservables", () => {
test("it should throw an error when method is not a spy", () => {
const fn = () => undefined;
expect(() => autoMockerPlus.withReturnObservables(fn, [])).toThrow();
});

test('it should return new observables on each call', (done) => {
const values = [false, true];

autoMockerPlus.withReturnObservables(mock.getObservable$, values);
const resArray: Observable<boolean>[] = [];
for (let i = 0; i < values.length; i++) {
resArray.push(mock.getObservable$());
}
forkJoin(resArray).subscribe((results) => {
expect(results.length).toEqual(values.length);
expect(results).toEqual(values);
done();
});
});

test("should return new observables when input is an observable", (done) => {
const values = [of(true), of(false)];

autoMockerPlus.withReturnObservables(mock.getObservable$, values);
const resultsArray: Observable<boolean>[] = [];

for (let i = 0; i < values.length; i++) {
resultsArray.push(mock.getObservable$());
}

forkJoin(resultsArray).subscribe((results) => {
expect(results.length).toEqual(values.length);
done();
});
});
});

describe('withReturnThrowObservable', () => {
test('should throw error when method is not a spy', () => {
const fn = () => undefined;
expect(() => autoMockerPlus.withReturnThrowObservable(fn)).toThrow();
});

test('should return observable with error on call', () => {
autoMockerPlus.withReturnThrowObservable(mock.getObservable$, "i am an error");

const result$ = mock.getObservable$();
const result = readObservableErrorSynchronously(result$);
expect(result.message).toEqual('i am an error');
});
});

describe('withFirstArgMappedReturnObservable', () => {
test('should throw error when method is not a spy', () => {
const fn = () => undefined;
expect(() => autoMockerPlus.withFirstArgMappedReturnObservable(fn, {})).toThrow();
});

test('should return mapped value when key is found', (done) => {
const map = {
false: true
};

autoMockerPlus.withFirstArgMappedReturnObservable(mock.getUpdatedValue, map, false);

const result = mock.getUpdatedValue('false');

result.subscribe((res) => {
expect(res).toEqual(true);
done();
});
});

test('should return default value when key not found', (done) => {
const map = {
false: true
};

autoMockerPlus.withFirstArgMappedReturnObservable(mock.getUpdatedValue, map, false);

const res = mock.getUpdatedValue('test key');

res.subscribe((result) => {
expect(result).toEqual(false);
done();
});
});
});

describe("withReturnSubjectForObservableProperty", () => {
test('should return value if initial value supplied', (done) => {
autoMockerPlus.withReturnSubjectForObservableProperty(mock, 'testProperty$', "silly silly baby");
mock.testProperty$.subscribe((result) => {
expect(result).toEqual('silly silly baby');
done();
});
});
});

describe('withReturnObservableForPropertyName', () => {
test('should return value when initial value supplied', (done) => {
autoMockerPlus.withReturnObservableForPropertyName(mock, "testProperty$", "my wife is cute");
mock.testProperty$.subscribe((result) => {
expect(result).toEqual('my wife is cute');
done();
})
})
})
})
9 changes: 4 additions & 5 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
transform: {},
testRunner: 'jest-jasmine2',
collectCoverage: true,
coverageProvider: 'v8',
coverageDirectory: './test-results/coverage',
coverageThreshold: {
global: {
branches: 50,
functions: 50,
lines: 50,
statements: 50
branches: 75,
functions: 75,
lines: 75,
statements: 75
}
},
reporters: ['default', 'jest-junit']
Expand Down
33 changes: 2 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ez-budgets/test-utils",
"version": "1.0.1",
"version": "1.0.2",
"description": "Packaging meant to help with the process of writing Jest tests",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down Expand Up @@ -39,7 +39,6 @@
"@babel/preset-typescript": "^7.23.3",
"@types/jest": "^29.5.11",
"@types/lodash": "^4.14.202",
"jest-jasmine2": "^29.7.0",
"jest-junit": "^16.0.0",
"ts-node": "^10.9.2",
"tsup": "^8.0.1",
Expand Down
6 changes: 3 additions & 3 deletions src/autoMockerPlus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ export class AutoMockerPlus extends AutoMocker {

public withReturnObservables<T>(
spy: (...args: any[]) => Observable<T>,
resolveWith: T[],
resolveWith: T[] | Observable<T>[],
spyName?: string
): Observable<T>[] {
if (this.isSpyLike(spy)) {
const observables: Observable<T>[] = resolveWith.map((r) => {
const observables: Observable<T>[] = resolveWith.map((r: T | Observable<T>) => {
if (r instanceof Observable) {
return r;
}
Expand All @@ -84,7 +84,7 @@ export class AutoMockerPlus extends AutoMocker {
spyName?: string
): Observable<T> {
if (this.isSpyLike(spy)) {
const observable: Observable<T> = throwError(error);
const observable: Observable<T> = throwError(() => new Error(error));
spy.mockReturnValue(observable);
return observable;
}
Expand Down

0 comments on commit 9f63720

Please sign in to comment.