-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0a4a7ba
commit eeff51a
Showing
9 changed files
with
116 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,86 @@ | ||
import { AutoMockerPlus } from "../src"; | ||
import { AutoMockerPlus, readObservableSynchronously } from "../src"; | ||
import { Observable, of } from "rxjs"; | ||
import { TestEmissionsCounter } from "../src/test-emissions-counter"; | ||
|
||
class TestMockClass { | ||
public getObservable$(): Observable<boolean> { | ||
return of(true); | ||
} | ||
} | ||
|
||
describe('AutoMockerPlus', () => { | ||
let autoMockerPlus: AutoMockerPlus; | ||
let mock: TestMockClass; | ||
|
||
beforeEach(() => { | ||
autoMockerPlus = new AutoMockerPlus(); | ||
mock = autoMockerPlus.mockClass(TestMockClass); | ||
}); | ||
|
||
describe("withReturnObservable", () => { | ||
test("should throw error if method is not a spy", () => { | ||
const spy = () => undefined; | ||
|
||
try { | ||
const result = autoMockerPlus.withReturnObservable(spy); | ||
fail(`should have thrown error but got: ${result}`) | ||
} catch (e) { | ||
expect(e).toBeTruthy(); | ||
expect(e.message).toBeTruthy(); | ||
expect((e.message as string).includes("not an actual spy")).toEqual(true); | ||
} | ||
}); | ||
|
||
[ | ||
true, | ||
false, | ||
undefined | ||
].forEach((testCase) => { | ||
test("should return an observable with the specified value", () => { | ||
const result = autoMockerPlus.withReturnObservable(mock.getObservable$, testCase); | ||
|
||
expect(result).toBeTruthy(); | ||
const value = readObservableSynchronously(result); | ||
expect(value).toEqual(testCase); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("withReturnNonEmittingObservable", () => { | ||
test("should throw error if method is not a spy", () => { | ||
const spy = () => undefined; | ||
|
||
expect(() => autoMockerPlus.withReturnNonEmittingObservable(spy)).toThrow(); | ||
}); | ||
|
||
test('observable should not emit', () => { | ||
autoMockerPlus.withReturnNonEmittingObservable(mock.getObservable$); | ||
|
||
const emissionsCounter = new TestEmissionsCounter(mock.getObservable$()); | ||
emissionsCounter.emissionsCountingObservable$.subscribe(); | ||
expect(emissionsCounter.emissions).toEqual(0); | ||
}); | ||
}); | ||
|
||
test.todo('build out plus level tests'); | ||
describe("withReturnCompletingCountedObservable", () => { | ||
test("should throw error if method is not a spy", () => { | ||
const spy = () => undefined; | ||
|
||
expect(() => autoMockerPlus.withReturnCompletingCountedObservable(spy)).toThrow(); | ||
}); | ||
|
||
test("should return a subscribed observable that does not complete", (done) => { | ||
const counter = autoMockerPlus.withReturnCompletingCountedObservable(mock.getObservable$, true); | ||
|
||
counter.countedObservable$.subscribe((result) => { | ||
expect(result).toEqual(true); | ||
expect(counter.activeSubscriptionCount).toEqual(1); | ||
done(); | ||
}); | ||
expect(counter.activeSubscriptionCount).toEqual(0); | ||
expect(counter.hadSubscribers).toEqual(true); | ||
expect(counter.hasActiveSubscribers).toEqual(false); | ||
expect(counter.allSubscriptionsFinalized).toEqual(true); | ||
}); | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
describe('readObservableSynchronously', () => { | ||
test.todo('build out observable tests') | ||
}) | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -248,5 +248,4 @@ export class AutoMockerPlus extends AutoMocker { | |
} | ||
return this.throwNotASpyError(spyName); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Observable, Subscriber, tap } from "rxjs"; | ||
|
||
export class TestEmissionsCounter<T> { | ||
private _totalEmissions = 0; | ||
|
||
public readonly emissionsCountingObservable$ = new Observable<T>((observer: Subscriber<T>) => { | ||
const subscription = this.observable$ | ||
.pipe(tap(() => this._totalEmissions++)) | ||
.subscribe(observer); | ||
|
||
return () => subscription.unsubscribe(); | ||
}) | ||
|
||
constructor(private readonly observable$: Observable<T>) {} | ||
|
||
public get emissions(): number { | ||
return this._totalEmissions; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters