From efa03f106fd076eedae89f356bf494b4a1b524aa Mon Sep 17 00:00:00 2001 From: hideokamoto Date: Thu, 7 Nov 2019 10:51:37 +0900 Subject: [PATCH] test: update test --- .eslintignore | 1 + __tests__/index.test.ts | 74 +++++++++++++++++++++++++++++++++++++++-- libs/index.ts | 3 +- 3 files changed, 74 insertions(+), 4 deletions(-) diff --git a/.eslintignore b/.eslintignore index dfe4faa..de86c56 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,2 +1,3 @@ docs/ examples/ +__tests__/ \ No newline at end of file diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index 3d52f7a..6257c75 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -1,8 +1,76 @@ -import func from '../libs/index' +import CloudFrontUpdator, { + interfaces +} from '../libs/index' +const condition: interfaces.Config = { + debugMode: true, + taskType: 'parallel', + allowSensitiveAction: false +} +class DummyClient { + // @ts-ignore + getDistributionConfig (param: any) { + return { + promise (): AWS.CloudFront.GetDistributionConfigResult { + const DistributionConfig: AWS.CloudFront.DistributionConfig = { + Enabled: true + } as AWS.CloudFront.DistributionConfig + return { + ETag: 'tag', + DistributionConfig + } + } + } + } + + // @ts-ignore + updateDistribution (param: any) { + return { + promise () { + return {} + } + } + } + + // @ts-ignore + listDistributions (param: any) { + return { + promise () { + return {} + } + } + } +} describe('dummy', () => { - it('test', () => { - expect(func('John')).toEqual('Hello John !') + // @ts-ignore + const updator: interfaces.UpdatorFunction = (id, conf) => conf + const filter: interfaces.FilterCondition = () => true + let client = new CloudFrontUpdator({ + updator, + filter + }, condition, { + cfClient: new DummyClient() as any as AWS.CloudFront + }) + it('should work', async () => { + const dist = {} as AWS.CloudFront.Distribution + await expect(client.updateDistribution(dist)).resolves.toBe(undefined) + }) + it('should throw error', async () => { + client = new CloudFrontUpdator({ + updator: (id, conf) => { + console.log(id) + conf.Enabled = false + return conf + }, + filter + }, condition, { + cfClient: new DummyClient() as any as AWS.CloudFront }) + const dist = { + Id: 'ETEST' + } as AWS.CloudFront.Distribution + const promise = client.updateDistribution(dist) + await expect(promise).rejects.toMatch("You can not allow the action, please set 'allowSensitiveAction' option in the constructor") + }) }) diff --git a/libs/index.ts b/libs/index.ts index 4a48f50..76d5f01 100644 --- a/libs/index.ts +++ b/libs/index.ts @@ -111,9 +111,10 @@ export class CloudFrontUpdator { }).promise() if (!DistributionConfig) throw new Error('No such distribution') if (!ETag) throw new Error('no ETag') + const beforeConfig = Object.assign({}, DistributionConfig) const config = await this.updator(distribution.Id, DistributionConfig) if (!this.allowSensitiveAction && config) { - if (DistributionConfig.Enabled !== config.Enabled) { + if (beforeConfig.Enabled !== config.Enabled) { const err = 'You can not allow the action, please set \'allowSensitiveAction\' option in the constructor' this.log(err) throw err