From f7532bcf1cbadd518a5f38ad8f0b9228e86a20ac Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 7 Aug 2020 11:05:04 +0200 Subject: [PATCH] config-loader: added tests for resolver --- .../config-loader/src/lib/resolver.test.ts | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 packages/config-loader/src/lib/resolver.test.ts diff --git a/packages/config-loader/src/lib/resolver.test.ts b/packages/config-loader/src/lib/resolver.test.ts new file mode 100644 index 0000000000..78e869e612 --- /dev/null +++ b/packages/config-loader/src/lib/resolver.test.ts @@ -0,0 +1,116 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const pathExists = jest.fn(); + +jest.mock('fs-extra', () => ({ pathExists })); + +import { resolveStaticConfig } from './resolver'; + +describe('resolveStaticConfig', () => { + afterEach(() => { + jest.resetAllMocks(); + }); + + it('should resolve no files for empty roots', async () => { + const resolved = await resolveStaticConfig({ + env: 'development', + rootPaths: [], + }); + + expect(resolved).toEqual([]); + expect(pathExists).not.toHaveBeenCalled(); + }); + + it('should resolve a single app-config', async () => { + pathExists.mockImplementation(async (path: string) => + ['/repo/app-config.yaml'].includes(path), + ); + const resolved = await resolveStaticConfig({ + env: 'development', + rootPaths: ['/repo'], + }); + + expect(resolved).toEqual(['/repo/app-config.yaml']); + expect(pathExists).toHaveBeenCalledTimes(4); + }); + + it('should resolve a app-configs in different directories', async () => { + pathExists.mockImplementation(async (path: string) => + ['/repo/app-config.yaml', '/repo/packages/a/app-config.yaml'].includes( + path, + ), + ); + const resolved = await resolveStaticConfig({ + env: 'development', + rootPaths: [ + '/repo/packages/a', + '/repo/packages/b', + '/other-repo', + '/repo', + ], + }); + + expect(resolved).toEqual([ + '/repo/packages/a/app-config.yaml', + '/repo/app-config.yaml', + ]); + expect(pathExists).toHaveBeenCalledTimes(16); + }); + + it('should resolve env and local configs', async () => { + pathExists.mockImplementation(async (path: string) => + [ + '/repo/app-config.yaml', + '/repo/app-config.local.yaml', + '/repo/app-config.production.yaml', + '/repo/app-config.production.local.yaml', + '/repo/app-config.development.local.yaml', + '/repo/packages/a/app-config.development.yaml', + '/repo/packages/a/app-config.local.yaml', + ].includes(path), + ); + const resolved = await resolveStaticConfig({ + env: 'development', + rootPaths: ['/repo/packages/a', '/repo'], + }); + + expect(resolved).toEqual([ + '/repo/packages/a/app-config.development.yaml', + '/repo/packages/a/app-config.local.yaml', + '/repo/app-config.development.local.yaml', + '/repo/app-config.local.yaml', + '/repo/app-config.yaml', + ]); + expect(pathExists).toHaveBeenCalledTimes(8); + }); + + it('resolves suffixed configs in the correct order', async () => { + pathExists.mockImplementation(async () => true); + const resolved = await resolveStaticConfig({ + env: 'production', + rootPaths: ['/repo'], + }); + + expect(resolved).toEqual([ + '/repo/app-config.production.local.yaml', + '/repo/app-config.production.yaml', + '/repo/app-config.local.yaml', + '/repo/app-config.yaml', + ]); + expect(pathExists).toHaveBeenCalledTimes(4); + }); +});