|
1 |
| -import {instance, mock, verify} from "../src/ts-mockito"; |
| 1 | +import {instance, mock, verify, when} from "../src/ts-mockito"; |
2 | 2 | import {MethodCallToStringConverter} from "../src/utils/MethodCallToStringConverter";
|
3 | 3 | import {Bar} from "./utils/Bar";
|
4 | 4 | import {Foo} from "./utils/Foo";
|
@@ -774,6 +774,59 @@ describe("verifying mocked object", () => {
|
774 | 774 | });
|
775 | 775 | });
|
776 | 776 | });
|
| 777 | + |
| 778 | + describe("with timeout", () => { |
| 779 | + it("should succeed if call already happend", async () => { |
| 780 | + // given |
| 781 | + foo.getBar(); |
| 782 | + |
| 783 | + // when |
| 784 | + await verify(mockedFoo.getBar()).timeout(10000); |
| 785 | + |
| 786 | + // then |
| 787 | + verify(mockedFoo.getBar()).once(); |
| 788 | + }); |
| 789 | + |
| 790 | + it("should wait for call to happen", async () => { |
| 791 | + // given |
| 792 | + setTimeout(() => foo.getBar(), 10); |
| 793 | + |
| 794 | + // when |
| 795 | + await verify(mockedFoo.getBar()).timeout(10000); |
| 796 | + |
| 797 | + // then |
| 798 | + verify(mockedFoo.getBar()).once(); |
| 799 | + }); |
| 800 | + |
| 801 | + it("should fail if call does not happen", async () => { |
| 802 | + // given |
| 803 | + |
| 804 | + // when |
| 805 | + let error; |
| 806 | + try { |
| 807 | + await verify(mockedFoo.getBar()).timeout(10); |
| 808 | + } catch (e) { |
| 809 | + error = e; |
| 810 | + } |
| 811 | + |
| 812 | + // then |
| 813 | + expect(error.message).toContain("to be called within"); |
| 814 | + }); |
| 815 | + |
| 816 | + it("should not alter call expectations", async () => { |
| 817 | + // given |
| 818 | + let result: string; |
| 819 | + when(mockedFoo.getBar()).thenReturn("abc"); |
| 820 | + setTimeout(() => result = foo.getBar(), 10); |
| 821 | + |
| 822 | + // when |
| 823 | + await verify(mockedFoo.getBar()).timeout(10000); |
| 824 | + |
| 825 | + // then |
| 826 | + expect(result).toEqual("abc"); |
| 827 | + verify(mockedFoo.getBar()).once(); |
| 828 | + }); |
| 829 | + }); |
777 | 830 | });
|
778 | 831 |
|
779 | 832 | function verifyCallCountErrorMessage(error, expectedCallCount, receivedCallCount): void {
|
|
0 commit comments