From 2b3a25f6d3f84e2a8f8f13ab0227f5324b956341 Mon Sep 17 00:00:00 2001 From: David LJ Date: Wed, 30 Oct 2024 16:34:27 +0100 Subject: [PATCH] test: fix test failing due to content filters --- .../index.spec.ts | 46 +++++++------------ .../testing/replacements.ts | 4 ++ 2 files changed, 20 insertions(+), 30 deletions(-) diff --git a/projects/ngx-meta/schematics/migrations/tree-shakeable-manager-providers/index.spec.ts b/projects/ngx-meta/schematics/migrations/tree-shakeable-manager-providers/index.spec.ts index a4b7f8265..e35cb5f76 100644 --- a/projects/ngx-meta/schematics/migrations/tree-shakeable-manager-providers/index.spec.ts +++ b/projects/ngx-meta/schematics/migrations/tree-shakeable-manager-providers/index.spec.ts @@ -29,11 +29,6 @@ describe('Tree shakeable manager providers migration', () => { let runner: SchematicTestRunner let tree: UnitTestTree let logWarnSpy: jest.Spied<(typeof logging.Logger.prototype)['warn']> - const [SAMPLE_OLD_IDENTIFIER, SAMPLE_NEW_IDENTIFIER, SAMPLE_ENTRYPOINT] = [ - Object.keys(STANDARD_REPLACEMENTS.identifierReplacements)[0], - Object.values(STANDARD_REPLACEMENTS.identifierReplacements)[0], - 'standard', - ] const SAMPLE_TYPESCRIPT_FILE_PATH = '/index.ts' beforeEach(async () => { @@ -180,22 +175,24 @@ describe('Tree shakeable manager providers migration', () => { }) describe('when namespace imports are used to import the library', () => { + const [SAMPLE_OLD_IDENTIFIER, SAMPLE_NEW_IDENTIFIER, SAMPLE_ENTRYPOINT] = [ + ...STANDARD_REPLACEMENTS.sampleReplacement, + 'standard', + ] const namespaceImport = `import ngxMetaMetadataModule from '${LIB_NAME}/${SAMPLE_ENTRYPOINT}'` - const oldUsage = `ngxMetaMetadataModule.${SAMPLE_OLD_IDENTIFIER}` - const newUsage = `ngxMetaMetadataModule.${SAMPLE_NEW_IDENTIFIER}()` + const fileWithOldUsage = ` + ${namespaceImport} - it('should leave the import as is', async () => { - tree.create(SAMPLE_TYPESCRIPT_FILE_PATH, namespaceImport) + const providers = [ngxMetaMetadataModule.${SAMPLE_OLD_IDENTIFIER}] + ` + const fileWithNewUsage = ` + ${namespaceImport} - await runMigration() - - expect(tree.readContent(SAMPLE_TYPESCRIPT_FILE_PATH)).toEqual( - namespaceImport, - ) - }) + const providers = [ngxMetaMetadataModule.${SAMPLE_NEW_IDENTIFIER}()] + ` it('should warn about it', async () => { - tree.create(SAMPLE_TYPESCRIPT_FILE_PATH, namespaceImport) + tree.create(SAMPLE_TYPESCRIPT_FILE_PATH, fileWithOldUsage) await runMigration() @@ -204,24 +201,13 @@ describe('Tree shakeable manager providers migration', () => { ) }) - it('should replace those usages too', async () => { - tree.create( - SAMPLE_TYPESCRIPT_FILE_PATH, - `${namespaceImport} - - const providers = [ - ${oldUsage}, - ]`, - ) + it('should replace those usages too, leaving import as is', async () => { + tree.create(SAMPLE_TYPESCRIPT_FILE_PATH, fileWithOldUsage) await runMigration() expect(tree.readContent(SAMPLE_TYPESCRIPT_FILE_PATH)).toEqual( - `${namespaceImport} - - const providers = [ - ${newUsage}, - ]`, + fileWithNewUsage, ) }) }) diff --git a/projects/ngx-meta/schematics/migrations/tree-shakeable-manager-providers/testing/replacements.ts b/projects/ngx-meta/schematics/migrations/tree-shakeable-manager-providers/testing/replacements.ts index e98ff0c47..68206e1b1 100644 --- a/projects/ngx-meta/schematics/migrations/tree-shakeable-manager-providers/testing/replacements.ts +++ b/projects/ngx-meta/schematics/migrations/tree-shakeable-manager-providers/testing/replacements.ts @@ -18,6 +18,10 @@ export class ModuleReplacements { .map((newIdentifier) => `${newIdentifier}()`) .join(',\n') } + + get sampleReplacement() { + return Object.entries(this.identifierReplacements)[0] + } } export const JSON_LD_REPLACEMENTS: ModuleReplacements = new ModuleReplacements({