Skip to content

Commit

Permalink
fix(transformers): handle template literals in styles or styleUrl pro…
Browse files Browse the repository at this point in the history
…perty (#2735) (#2736)

fixes #2735
  • Loading branch information
skrtheboss committed Sep 12, 2024
1 parent 2b28777 commit 03cae4c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
21 changes: 20 additions & 1 deletion e2e/ng-jit-transformers/__tests__/replace-resource.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TestBed } from '@angular/core/testing';

import { BarComponent, DATA_TOKEN } from '../bar.component';
import { BarComponent, StringStylesBarComponent, DATA_TOKEN } from '../bar.component';

test('templateUrl/styleUrls/styles should work', () => {
TestBed.configureTestingModule({
Expand All @@ -20,3 +20,22 @@ test('templateUrl/styleUrls/styles should work', () => {
expect(window.getComputedStyle(elementToFind).color).toEqual('');
expect(window.getComputedStyle(elementToFind).fontSize).toEqual('');
});

test('styleUrl/styles string should work', () => {
TestBed.configureTestingModule({
imports: [StringStylesBarComponent],
providers: [
{
provide: DATA_TOKEN,
useValue: {},
},
],
});
const fixture = TestBed.createComponent(StringStylesBarComponent);
fixture.detectChanges();

const elementToFind = fixture.debugElement.nativeElement.querySelector('p');
expect(elementToFind).toBeDefined();
expect(window.getComputedStyle(elementToFind).color).toEqual('');
expect(window.getComputedStyle(elementToFind).fontSize).toEqual('');
});
16 changes: 16 additions & 0 deletions e2e/ng-jit-transformers/bar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,19 @@ export const DATA_TOKEN = new InjectionToken<ServerError>('DataToken');
export class BarComponent {
constructor(@Inject(DATA_TOKEN) public data: ServerError) {}
}

@Component({
standalone: true,
selector: 'bar',
templateUrl: `./bar.component.html`,
styleUrl: `./bar.component.scss`,
// we have to setup styles this way, since simple styles/styleUrs properties will be removed (jest does not unit test styles)
styles: `
p {
color: red;
}
`,
})
export class StringStylesBarComponent {
constructor(@Inject(DATA_TOKEN) public data: ServerError) {}
}
4 changes: 2 additions & 2 deletions src/transformers/replace-resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ function visitComponentMetadata(
return nodeFactory.updatePropertyAssignment(node, nodeFactory.createIdentifier(TEMPLATE), importName);

case STYLES:
if (!ts.isArrayLiteralExpression(node.initializer) && !ts.isStringLiteral(node.initializer)) {
if (!ts.isArrayLiteralExpression(node.initializer) && !ts.isStringLiteralLike(node.initializer)) {
return node;
}

Expand All @@ -250,7 +250,7 @@ function visitComponentMetadata(
return undefined;

case STYLE_URL:
if (!ts.isStringLiteral(node.initializer)) {
if (!ts.isStringLiteralLike(node.initializer)) {
return node;
}

Expand Down

0 comments on commit 03cae4c

Please sign in to comment.