From 12e8a708a244625a5623cee387d118ee5ac7b3a9 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Sun, 5 Jan 2025 09:56:28 +0100 Subject: [PATCH] tests: add is env project basic tests --- .../targets/environment.targets.unit-test.ts | 173 ++++++++++++------ 1 file changed, 117 insertions(+), 56 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/targets/environment.targets.unit-test.ts b/projects/nx-verdaccio/src/plugin/targets/environment.targets.unit-test.ts index e529395..9d7a21d 100644 --- a/projects/nx-verdaccio/src/plugin/targets/environment.targets.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/targets/environment.targets.unit-test.ts @@ -1,61 +1,122 @@ -import { describe } from 'vitest'; +import { describe, expect } from 'vitest'; +import { ProjectConfiguration } from '@nx/devkit'; +import { isEnvProject } from './environment.targets'; +import { NormalizedCreateNodeOptions } from '../normalize-create-nodes-options'; describe('isEnvProject', () => { - //export function isEnvProject( - // projectConfig: ProjectConfiguration, - // options: NormalizedCreateNodeOptions['environments'] - // ): boolean { - // const { tags: existingTags = [], targets } = projectConfig; - // const existingTargetNames = Object.keys(targets ?? {}); - // const { - // filterByTags: environmentsTagFilters, - // targetNames: environmentTargetNames, - // } = options; - // - - - // if (!existingTargetNames || !environmentTargetNames) { - // return false; - // } - // - // if ( - // existingTargetNames.some((existingTarget) => - // environmentTargetNames.includes(existingTarget) - // ) - // ) { - // if (existingTags && environmentsTagFilters) { - // return existingTags.some((existingTag) => - // environmentsTagFilters.includes(existingTag) - // ); - // } - // return true; - // } - // - // return false; - // } - - // IT test - // if (!existingTargetNames || !environmentTargetNames) { - // return false; - // } - - // IT test - // if ( - // existingTargetNames.some((existingTarget) => - // environmentTargetNames.includes(existingTarget) - // ) - // ) { - // IT test - // if (existingTags && environmentsTagFilters) { - // return existingTags.some((existingTag) => - // environmentsTagFilters.includes(existingTag) - // ); - // } - - // return true; - // } - // IT test - // // return false; + const projectConfig: ProjectConfiguration = { + root: '', + tags: ['env:production', 'type:library'], + targets: {} + }; + const normalizedCreateNodeOptions: NormalizedCreateNodeOptions['environments'] = { + environmentsDir: '', + targetNames: null, + filterByTags: ['env:production'] + }; + it('should return false if existingTargetNames are not present', () => { + const options = {...normalizedCreateNodeOptions, targetNames: ['mockTarget']} + const config = {...projectConfig, targets: null} + const result = isEnvProject(config, options); + expect(result).toBe(false); + }); + + it('should return false if environmentTargetNames are not present', () => { + const config = { + ...projectConfig, + targets: { + build: {}, + test: {}, + } + } + const result = isEnvProject(config, normalizedCreateNodeOptions); + expect(result).toBe(false); + }); + + it('should return false if existingTargetNames, and existingTargetNames are not present ', () => { + const result = isEnvProject(projectConfig, normalizedCreateNodeOptions); + expect(result).toBe(false); + }); + + + it('should return false if none of the existingTargetNames match any of the environmentTargetNames', () => { + const config = { + ...projectConfig, + targets: { + build: {}, + test: {}, + } + } + const options = {...normalizedCreateNodeOptions, targetNames: ['mockTarget']} + const result = isEnvProject(config, options); + expect(result).toBe(false); + }); + + it('should return true if any existingTargetNames match environmentTargetNames and no tags', () => { + // i have to set tags to null + const config = { + ...projectConfig, + tags: null, + targets: { + build: {}, + test: {}, + } + } + const options = {...normalizedCreateNodeOptions, targetNames: ['cola', 'mock', 'build']} + const result = isEnvProject(config, options); + expect(result).toBe(true); + }); + + it('should return true if any existingTargetNames match environmentTargetNames and no filterByTags', () => { + // i have to set filter by tags to null + const config = { + ...projectConfig, + targets: { + build: {}, + test: {}, + } + } + const options = { + ...normalizedCreateNodeOptions, + targetNames: ['cola', 'mock', 'build'], + filterByTags: null + } + const result = isEnvProject(config, options); + expect(result).toBe(true); + }); + + it('should return true if any existingTargetNames match environmentTargetNames and existingTags are present with environmentsTagFilters, and any existingTags match any environmentsTagFilters', () => { + const config = { + ...projectConfig, + targets: { + build: {}, + test: {}, + } + } + const options = { + ...normalizedCreateNodeOptions, + targetNames: ['cola', 'mock', 'build'], + } + const result = isEnvProject(config, options); + expect(result).toBe(true); + }); + + it('should return false if any existingTargetNames match environmentTargetNames and existingTags are present with environmentsTagFilters, and NONE existingTags match any environmentsTagFilters', () => { + const config = { + ...projectConfig, + targets: { + build: {}, + test: {}, + } + } + const options = { + ...normalizedCreateNodeOptions, + targetNames: ['cola', 'mock', 'build'], + filterByTags: ['mock-tag-no-match'] + } + const result = isEnvProject(config, options); + expect(result).toBe(false); + }); })