|
1 | 1 | import { describe, expect, it } from 'vitest'; |
| 2 | +import { AwsCredentials } from '../../../src/auth/AwsCredentials'; |
| 3 | +import { OnlineStatus } from '../../../src/services/OnlineStatus'; |
2 | 4 | import { OnlineFeatureErrorCode } from '../../../src/utils/OnlineFeatureError'; |
3 | 5 | import { OnlineFeatureGuard } from '../../../src/utils/OnlineFeatureGuard'; |
4 | 6 |
|
5 | 7 | describe('OnlineFeatureGuard', () => { |
6 | | - it('should pass when both internet and auth are available', () => { |
7 | | - const onlineStatus = { isOnline: true }; |
8 | | - const awsCredentials = { credentialsAvailable: () => true }; |
9 | | - const guard = new OnlineFeatureGuard(onlineStatus as any, awsCredentials as any); |
| 8 | + it('should pass when both internet and auth are available', async () => { |
| 9 | + const onlineStatus = { checkNow: () => Promise.resolve(true) } as OnlineStatus; |
| 10 | + const awsCredentials = { credentialsAvailable: () => true } as AwsCredentials; |
| 11 | + const guard = new OnlineFeatureGuard(onlineStatus, awsCredentials); |
10 | 12 |
|
11 | | - expect(() => guard.check({ requiresInternet: true, requiresAuth: true })).not.toThrow(); |
| 13 | + await expect(guard.check({ requiresInternet: true, requiresAuth: true })).resolves.not.toThrow(); |
12 | 14 | }); |
13 | 15 |
|
14 | | - it('should throw NoInternet when internet is required but not available', () => { |
15 | | - const onlineStatus = { isOnline: false }; |
16 | | - const awsCredentials = { credentialsAvailable: () => true }; |
17 | | - const guard = new OnlineFeatureGuard(onlineStatus as any, awsCredentials as any); |
| 16 | + it('should throw NoInternet when internet is required but not available', async () => { |
| 17 | + const onlineStatus = { checkNow: () => Promise.resolve(false) } as OnlineStatus; |
| 18 | + const awsCredentials = { credentialsAvailable: () => true } as AwsCredentials; |
| 19 | + const guard = new OnlineFeatureGuard(onlineStatus, awsCredentials); |
18 | 20 |
|
19 | | - expect(() => guard.check({ requiresInternet: true, requiresAuth: true })).toThrow( |
| 21 | + await expect(guard.check({ requiresInternet: true, requiresAuth: true })).rejects.toThrow( |
20 | 22 | expect.objectContaining({ code: OnlineFeatureErrorCode.NoInternet }), |
21 | 23 | ); |
22 | 24 | }); |
23 | 25 |
|
24 | | - it('should throw NoAuthentication when auth is required but not available', () => { |
25 | | - const onlineStatus = { isOnline: true }; |
26 | | - const awsCredentials = { credentialsAvailable: () => false }; |
27 | | - const guard = new OnlineFeatureGuard(onlineStatus as any, awsCredentials as any); |
| 26 | + it('should throw NoAuthentication when auth is required but not available', async () => { |
| 27 | + const onlineStatus = { checkNow: () => Promise.resolve(true) } as OnlineStatus; |
| 28 | + const awsCredentials = { credentialsAvailable: () => false } as AwsCredentials; |
| 29 | + const guard = new OnlineFeatureGuard(onlineStatus, awsCredentials); |
28 | 30 |
|
29 | | - expect(() => guard.check({ requiresInternet: true, requiresAuth: true })).toThrow( |
| 31 | + await expect(guard.check({ requiresInternet: true, requiresAuth: true })).rejects.toThrow( |
30 | 32 | expect.objectContaining({ code: OnlineFeatureErrorCode.NoAuthentication }), |
31 | 33 | ); |
32 | 34 | }); |
33 | 35 |
|
34 | | - it('should pass when internet is not required and not available', () => { |
35 | | - const onlineStatus = { isOnline: false }; |
36 | | - const awsCredentials = { credentialsAvailable: () => true }; |
37 | | - const guard = new OnlineFeatureGuard(onlineStatus as any, awsCredentials as any); |
| 36 | + it('should pass when internet is not required and not available', async () => { |
| 37 | + const onlineStatus = { checkNow: () => Promise.resolve(false) } as OnlineStatus; |
| 38 | + const awsCredentials = { credentialsAvailable: () => true } as AwsCredentials; |
| 39 | + const guard = new OnlineFeatureGuard(onlineStatus, awsCredentials); |
38 | 40 |
|
39 | | - expect(() => guard.check({ requiresInternet: false, requiresAuth: true })).not.toThrow(); |
| 41 | + await expect(guard.check({ requiresInternet: false, requiresAuth: true })).resolves.not.toThrow(); |
40 | 42 | }); |
41 | 43 |
|
42 | | - it('should pass when auth is not required and not available', () => { |
43 | | - const onlineStatus = { isOnline: true }; |
44 | | - const awsCredentials = { credentialsAvailable: () => false }; |
45 | | - const guard = new OnlineFeatureGuard(onlineStatus as any, awsCredentials as any); |
| 44 | + it('should pass when auth is not required and not available', async () => { |
| 45 | + const onlineStatus = { checkNow: () => Promise.resolve(true) } as OnlineStatus; |
| 46 | + const awsCredentials = { credentialsAvailable: () => false } as AwsCredentials; |
| 47 | + const guard = new OnlineFeatureGuard(onlineStatus, awsCredentials); |
46 | 48 |
|
47 | | - expect(() => guard.check({ requiresInternet: true, requiresAuth: false })).not.toThrow(); |
| 49 | + await expect(guard.check({ requiresInternet: true, requiresAuth: false })).resolves.not.toThrow(); |
48 | 50 | }); |
49 | 51 |
|
50 | | - it('should check internet before auth', () => { |
51 | | - const onlineStatus = { isOnline: false }; |
52 | | - const awsCredentials = { credentialsAvailable: () => false }; |
53 | | - const guard = new OnlineFeatureGuard(onlineStatus as any, awsCredentials as any); |
| 52 | + it('should check internet before auth', async () => { |
| 53 | + const onlineStatus = { checkNow: () => Promise.resolve(false) } as OnlineStatus; |
| 54 | + const awsCredentials = { credentialsAvailable: () => false } as AwsCredentials; |
| 55 | + const guard = new OnlineFeatureGuard(onlineStatus, awsCredentials); |
54 | 56 |
|
55 | | - expect(() => guard.check({ requiresInternet: true, requiresAuth: true })).toThrow( |
| 57 | + await expect(guard.check({ requiresInternet: true, requiresAuth: true })).rejects.toThrow( |
56 | 58 | expect.objectContaining({ code: OnlineFeatureErrorCode.NoInternet }), |
57 | 59 | ); |
58 | 60 | }); |
|
0 commit comments