|
1 | 1 | // tslint:disable:no-empty |
2 | 2 | // tslint:disable:no-unnecessary-callback-wrapper |
3 | 3 |
|
4 | | -import { Config, Credentials, DynamoDB } from 'aws-sdk' |
5 | | -import { EMPTY, Observable } from 'rxjs' |
| 4 | +import { Config, Credentials } from 'aws-sdk' |
| 5 | +import { of } from 'rxjs' |
6 | 6 | import { resetDynamoEasyConfig } from '../../test/helper/resetDynamoEasyConfig.function' |
7 | 7 | import { updateDynamoEasyConfig } from '../config' |
8 | 8 | import { DynamoRx } from './dynamo-rx' |
9 | | -import { SessionValidityEnsurer } from './session-validity-ensurer.type' |
10 | 9 |
|
11 | 10 | describe('dynamo rx', () => { |
12 | | - describe('should call the validity ensurer before each call and return an observable', () => { |
| 11 | + describe('should call the validity ensurer before each call and call the correct dynamoDB method', () => { |
13 | 12 | let dynamoRx: DynamoRx |
14 | | - let spyValidityEnsurer: SessionValidityEnsurer |
| 13 | + let sessionValidityEnsurerSpy: jasmine.Spy |
| 14 | + let dynamoDbSpy: jasmine.Spy |
| 15 | + let pseudoParams: any |
15 | 16 |
|
16 | 17 | beforeEach(() => { |
17 | | - spyValidityEnsurer = jasmine.createSpy().and.returnValue(EMPTY) |
18 | | - updateDynamoEasyConfig({ sessionValidityEnsurer: spyValidityEnsurer }) |
| 18 | + pseudoParams = { TableName: 'tableName', KeyConditionExpression: 'blub' } |
| 19 | + sessionValidityEnsurerSpy = jasmine.createSpy().and.returnValue(of(true)) |
| 20 | + updateDynamoEasyConfig({ sessionValidityEnsurer: sessionValidityEnsurerSpy }) |
19 | 21 | dynamoRx = new DynamoRx() |
20 | 22 | }) |
21 | 23 |
|
22 | | - afterEach(resetDynamoEasyConfig) |
| 24 | + afterEach(() => { |
| 25 | + resetDynamoEasyConfig() |
| 26 | + expect(sessionValidityEnsurerSpy).toHaveBeenCalled() |
| 27 | + expect(dynamoDbSpy).toHaveBeenCalledTimes(1) |
| 28 | + expect(dynamoDbSpy).toHaveBeenCalledWith(pseudoParams) |
| 29 | + }) |
23 | 30 |
|
24 | | - it('putItem', () => { |
25 | | - expect(dynamoRx.putItem(<any>null) instanceof Observable).toBeTruthy() |
26 | | - expect(spyValidityEnsurer).toHaveBeenCalled() |
| 31 | + it('putItem', async () => { |
| 32 | + dynamoDbSpy = spyOn(dynamoRx.dynamoDb, 'putItem').and.returnValue({ promise: () => Promise.resolve() }) |
| 33 | + await dynamoRx.putItem(pseudoParams).toPromise() |
27 | 34 | }) |
28 | | - it('getItem', () => { |
29 | | - expect(dynamoRx.getItem(<any>null) instanceof Observable).toBeTruthy() |
30 | | - expect(spyValidityEnsurer).toHaveBeenCalled() |
| 35 | + |
| 36 | + it('getItem', async () => { |
| 37 | + dynamoDbSpy = spyOn(dynamoRx.dynamoDb, 'getItem').and.returnValue({ promise: () => Promise.resolve() }) |
| 38 | + await dynamoRx.getItem(pseudoParams).toPromise() |
31 | 39 | }) |
32 | | - it('updateItem', () => { |
33 | | - expect(dynamoRx.updateItem(<any>null) instanceof Observable).toBeTruthy() |
34 | | - expect(spyValidityEnsurer).toHaveBeenCalled() |
| 40 | + |
| 41 | + it('updateItem', async () => { |
| 42 | + dynamoDbSpy = spyOn(dynamoRx.dynamoDb, 'updateItem').and.returnValue({ promise: () => Promise.resolve() }) |
| 43 | + await dynamoRx.updateItem(pseudoParams).toPromise() |
35 | 44 | }) |
36 | | - it('deleteItem', () => { |
37 | | - expect(dynamoRx.deleteItem(<any>null) instanceof Observable).toBeTruthy() |
38 | | - expect(spyValidityEnsurer).toHaveBeenCalled() |
| 45 | + |
| 46 | + it('deleteItem', async () => { |
| 47 | + dynamoDbSpy = spyOn(dynamoRx.dynamoDb, 'deleteItem').and.returnValue({ promise: () => Promise.resolve() }) |
| 48 | + await dynamoRx.deleteItem(pseudoParams).toPromise() |
39 | 49 | }) |
40 | | - it('batchWriteItem', () => { |
41 | | - expect(dynamoRx.batchWriteItem(<any>null) instanceof Observable).toBeTruthy() |
42 | | - expect(spyValidityEnsurer).toHaveBeenCalled() |
| 50 | + |
| 51 | + it('batchWriteItem', async () => { |
| 52 | + dynamoDbSpy = spyOn(dynamoRx.dynamoDb, 'batchWriteItem').and.returnValue({ promise: () => Promise.resolve() }) |
| 53 | + await dynamoRx.batchWriteItem(pseudoParams).toPromise() |
43 | 54 | }) |
44 | | - it('batchGetItems', () => { |
45 | | - expect(dynamoRx.batchGetItems(<any>null) instanceof Observable).toBeTruthy() |
46 | | - expect(spyValidityEnsurer).toHaveBeenCalled() |
| 55 | + |
| 56 | + it('batchGetItems', async () => { |
| 57 | + dynamoDbSpy = spyOn(dynamoRx.dynamoDb, 'batchGetItem').and.returnValue({ promise: () => Promise.resolve() }) |
| 58 | + await dynamoRx.batchGetItems(pseudoParams).toPromise() |
47 | 59 | }) |
48 | | - it('transactWriteItem', () => { |
49 | | - expect(dynamoRx.transactWriteItems(<any>null) instanceof Observable).toBeTruthy() |
50 | | - expect(spyValidityEnsurer).toHaveBeenCalled() |
| 60 | + |
| 61 | + it('transactWriteItems', async () => { |
| 62 | + dynamoDbSpy = spyOn(dynamoRx.dynamoDb, 'transactWriteItems').and.returnValue({ promise: () => Promise.resolve() }) |
| 63 | + await dynamoRx.transactWriteItems(pseudoParams).toPromise() |
51 | 64 | }) |
52 | | - it('transactGetItems', () => { |
53 | | - expect(dynamoRx.transactGetItems(<any>null) instanceof Observable).toBeTruthy() |
54 | | - expect(spyValidityEnsurer).toHaveBeenCalled() |
| 65 | + |
| 66 | + it('transactGetItems', async () => { |
| 67 | + dynamoDbSpy = spyOn(dynamoRx.dynamoDb, 'transactGetItems').and.returnValue({ promise: () => Promise.resolve() }) |
| 68 | + await dynamoRx.transactGetItems(pseudoParams).toPromise() |
55 | 69 | }) |
56 | | - it('scan', () => { |
57 | | - expect(dynamoRx.scan(<any>null) instanceof Observable).toBeTruthy() |
58 | | - expect(spyValidityEnsurer).toHaveBeenCalled() |
| 70 | + |
| 71 | + it('scan', async () => { |
| 72 | + dynamoDbSpy = spyOn(dynamoRx.dynamoDb, 'scan').and.returnValue({ promise: () => Promise.resolve() }) |
| 73 | + await dynamoRx.scan(pseudoParams).toPromise() |
59 | 74 | }) |
60 | | - it('query', () => { |
61 | | - const params: DynamoDB.QueryInput = { |
62 | | - TableName: 'tableName', |
63 | | - } |
64 | | - expect(dynamoRx.query({ ...params, KeyConditionExpression: 'blub' }) instanceof Observable).toBeTruthy() |
65 | | - expect(spyValidityEnsurer).toHaveBeenCalled() |
66 | | - expect(() => dynamoRx.query(params)).toThrow() |
| 75 | + |
| 76 | + it('query', async () => { |
| 77 | + dynamoDbSpy = spyOn(dynamoRx.dynamoDb, 'query').and.returnValue({ promise: () => Promise.resolve() }) |
| 78 | + await dynamoRx.query(pseudoParams).toPromise() |
67 | 79 | }) |
68 | | - it('makeRequest', () => { |
69 | | - expect(dynamoRx.makeRequest(<any>null) instanceof Observable).toBeTruthy() |
70 | | - expect(spyValidityEnsurer).toHaveBeenCalled() |
| 80 | + }) |
| 81 | + |
| 82 | + describe('makeRequest', async () => { |
| 83 | + let dynamoRx: DynamoRx |
| 84 | + let sessionValidityEnsurerSpy: jasmine.Spy |
| 85 | + let dynamoDbSpy: jasmine.Spy |
| 86 | + let pseudoParams: any |
| 87 | + |
| 88 | + beforeEach(() => { |
| 89 | + pseudoParams = { TableName: 'tableName', KeyConditionExpression: 'blub' } |
| 90 | + sessionValidityEnsurerSpy = jasmine.createSpy().and.returnValue(of(true)) |
| 91 | + updateDynamoEasyConfig({ sessionValidityEnsurer: sessionValidityEnsurerSpy }) |
| 92 | + dynamoRx = new DynamoRx() |
| 93 | + }) |
| 94 | + |
| 95 | + afterEach(() => { |
| 96 | + resetDynamoEasyConfig() |
| 97 | + expect(sessionValidityEnsurerSpy).toHaveBeenCalled() |
| 98 | + expect(dynamoDbSpy).toHaveBeenCalledTimes(1) |
| 99 | + expect(dynamoDbSpy).toHaveBeenCalledWith('pseudoOperation', pseudoParams) |
| 100 | + }) |
| 101 | + |
| 102 | + it('should call the validity ensurer before each call and call the correct dynamoDB method', async () => { |
| 103 | + dynamoDbSpy = spyOn(dynamoRx.dynamoDb, 'makeRequest').and.returnValue({ promise: () => Promise.resolve() }) |
| 104 | + await dynamoRx.makeRequest('pseudoOperation', pseudoParams).toPromise() |
| 105 | + }) |
| 106 | + }) |
| 107 | + |
| 108 | + describe('query', () => { |
| 109 | + beforeEach(() => {}) |
| 110 | + it('should throw when no KeyConditionExpression was given', () => { |
| 111 | + const dynamoRx = new DynamoRx() |
| 112 | + expect(() => dynamoRx.query({ TableName: 'tableName' })).toThrow() |
71 | 113 | }) |
72 | 114 | }) |
73 | 115 |
|
|
0 commit comments