Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Final auto mocker tests #8

Merged
merged 2 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 134 additions & 1 deletion __tests__/auto-mocker-plus.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class TestMockClass {
}

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

public getPromise(): Promise<boolean> {
return Promise.resolve(true);
}
}

describe('AutoMockerPlus', () => {
Expand Down Expand Up @@ -215,7 +219,136 @@ describe('AutoMockerPlus', () => {
mock.testProperty$.subscribe((result) => {
expect(result).toEqual('my wife is cute');
done();
})
});
});
});

describe('withReturnSubjectWithCompletingCountedObservableForObservableProperty', () => {
test('should get counter and subject with no buffer when called and no value passed', () => {
const obj = autoMockerPlus.withReturnSubjectWithCompletingCountedObservableForObservableProperty(mock, 'testProperty$');
expect(obj.subject["_buffer"]).toEqual([]);
expect(obj.counter).toBeTruthy();
});

test("should get counter and subject with buffer when called and value passed", (done) => {
const obj = autoMockerPlus.withReturnSubjectWithCompletingCountedObservableForObservableProperty(mock, "testProperty$", "just a simple test string");
expect(obj.subject["_buffer"].length).toEqual(1);
expect(obj.counter).toBeTruthy();
mock.testProperty$.subscribe((result) => {
expect(result).toEqual('just a simple test string');
done();
});
});
});

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

test('should return observable and subject for updating values when called', (done) => {
const initial = false;
const subject = autoMockerPlus.withReturnSubjectAsObservable(mock.getObservable$);
let emissionNumber = 0;
mock.getObservable$().subscribe((result) => {
if (emissionNumber++ === 0) {
expect(result).toEqual(false)
} else {
expect(result).toEqual(true);
done();
}
});
subject.next(initial);
subject.next(true);
});
});

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

test('should return subject and observable when called', (done) => {
const subject = autoMockerPlus.withReturnReplaySubjectAsObservable(mock.getObservable$);
let emissions = 0;
mock.getObservable$().subscribe((result) => {
if (emissions++ === 0) {
expect(result).toEqual(false);
} else {
expect(result).toEqual(true);
done();
}
});
subject.next(false);
subject.next(true);
});

test('when called with initial value, result should be initial value', (done) => {
autoMockerPlus.withReturnReplaySubjectAsObservable(mock.getObservable$, true);
mock.getObservable$().subscribe((result) => {
expect(result).toEqual(true);
done();
});
});
});

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

test('observable should show error state when called with an error message', (done) => {
autoMockerPlus.withReturnSubjectWithErrorAsObservable(mock.getObservable$, "test error");
const result = mock.getObservable$();

result.subscribe({
next: (result) => {
fail(`should not have received result: ${result}`)
},
error: (err) => {
expect(err).toEqual('test error');
done();
}
});
});

test('observable should show error state with default error when called with no error message', (done) => {
autoMockerPlus.withReturnSubjectWithErrorAsObservable(mock.getObservable$);
const result = mock.getObservable$();

result.subscribe({
next: (result) => {
fail(`should not have received result: ${result}`);
},
error: (err) => {
expect(err.message).toEqual('error');
done();
}
});
});
});

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

test('should return a promise that resolves', () => {
autoMockerPlus.withReturnPromise(mock.getPromise, true);
expect(mock.getPromise()).resolves.toEqual(true);
});
});

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

test('should return a rejected promise', () => {
autoMockerPlus.withReturnRejectedPromise(mock.getPromise, "i got rejected");
expect(mock.getPromise()).rejects.toEqual("i got rejected");
})
})
})
8 changes: 4 additions & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ module.exports = {
coverageDirectory: './test-results/coverage',
coverageThreshold: {
global: {
branches: 75,
functions: 75,
lines: 75,
statements: 75
branches: 90,
functions: 93,
lines: 92,
statements: 92
}
},
reporters: ['default', 'jest-junit']
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": "@ez-budgets/test-utils",
"version": "1.0.2",
"version": "1.0.3",
"description": "Packaging meant to help with the process of writing Jest tests",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
4 changes: 0 additions & 4 deletions src/autoMockerPlus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,10 @@ export class AutoMockerPlus extends AutoMocker {

public withReturnSubjectAsObservable<T>(
spy: (...args: any[]) => Observable<T>,
resolveWith?: T,
spyName?: string,
): Subject<T> {
if (this.isSpyLike(spy)) {
const subject: Subject<T> = new Subject<T>();
if (resolveWith !== undefined) {
subject.next(resolveWith);
}
const observable: Observable<T> = subject.asObservable();
spy.mockReturnValue(observable);
return subject;
Expand Down
6 changes: 3 additions & 3 deletions src/read-observable-synchronously.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
if (!emitted) {
subscription.unsubscribe();
fail(
`observable did not emit (skips requested: ${skip}, total skipped emmissions: ${emissionCount})`,
`observable did not emit (skips requested: ${skip}, total skipped emissions: ${emissionCount})`,

Check warning on line 42 in src/read-observable-synchronously.ts

View check run for this annotation

Codecov / codecov/patch

src/read-observable-synchronously.ts#L42

Added line #L42 was not covered by tests
)
}

Expand Down Expand Up @@ -75,7 +75,7 @@
if (!emitted) {
subscription.unsubscribe();
fail(
`observable did not emit error (skips requested: ${skip}, total skipped emmissions: ${emissionCount})`,
`observable did not emit error (skips requested: ${skip}, total skipped emissions: ${emissionCount})`,

Check warning on line 78 in src/read-observable-synchronously.ts

View check run for this annotation

Codecov / codecov/patch

src/read-observable-synchronously.ts#L78

Added line #L78 was not covered by tests
);
}

Expand Down Expand Up @@ -112,7 +112,7 @@
if (!emitted) {
subscription.unsubscribe();
fail(
`observable did not emit complete (skips requested: ${skip}, total skipped emmissions: ${emissionCount})`,
`observable did not emit complete (skips requested: ${skip}, total skipped emissions: ${emissionCount})`,

Check warning on line 115 in src/read-observable-synchronously.ts

View check run for this annotation

Codecov / codecov/patch

src/read-observable-synchronously.ts#L115

Added line #L115 was not covered by tests
);
}

Expand Down