From 9c1ea70564f11323337646a6d74ebbddc8965a31 Mon Sep 17 00:00:00 2001 From: MG Date: Tue, 18 May 2021 19:36:50 +0200 Subject: [PATCH] fix(core): broken query selectors are properly normalized #567 --- .../render/generate-template.ts | 2 +- tests/issue-567/test.spec.ts | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 tests/issue-567/test.spec.ts diff --git a/libs/ng-mocks/src/lib/mock-component/render/generate-template.ts b/libs/ng-mocks/src/lib/mock-component/render/generate-template.ts index 4c04adfd74..ab7f501f3f 100644 --- a/libs/ng-mocks/src/lib/mock-component/render/generate-template.ts +++ b/libs/ng-mocks/src/lib/mock-component/render/generate-template.ts @@ -32,7 +32,7 @@ export default (queries?: Record): string => { continue; } if (typeof query.selector === 'string') { - const selector = query.selector.replace(new RegExp('[^a-z0-9-_]+', 'mg'), ' ').trim().replace(' ', '_'); + const selector = query.selector.replace(new RegExp('[^a-zA-Z0-9_]', 'mg'), '_'); queries[`__mockView_key_${selector}`] = new ViewChild(`key_${selector}`, viewChildArgs); queries[`__mockTpl_key_${selector}`] = query; parts.push(viewChildTemplate(selector, 'key')); diff --git a/tests/issue-567/test.spec.ts b/tests/issue-567/test.spec.ts new file mode 100644 index 0000000000..8131a4b61c --- /dev/null +++ b/tests/issue-567/test.spec.ts @@ -0,0 +1,19 @@ +import { Component, ContentChild, TemplateRef } from '@angular/core'; +import { MockBuilder, MockRender } from 'ng-mocks'; + +@Component({ + selector: 'target', + template: '', +}) +class TargetComponent { + @ContentChild('s-e_-_ri23sTo 12 o#ltTem_~_plate_', {} as any) + public readonly seriesTooltipTemplate?: TemplateRef; +} + +describe('issue-567', () => { + beforeEach(() => MockBuilder(null, TargetComponent)); + + it('does not fail on seriesTooltipTemplate', () => { + expect(() => MockRender(TargetComponent)).not.toThrow(); + }); +});