Skip to content

feat(duplicate-directive): adds duplicate directive #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions apps/www/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@
"error",
{
"type": "attribute",
"prefix": "app",
"prefix": [
"app",
"ngt"
],
"style": "camelCase"
}
],
"@angular-eslint/component-selector": [
"error",
{
"type": "element",
"prefix": "app",
"prefix": [
"app",
"ngt"
],
"style": "kebab-case"
}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {render, RenderComponentOptions, screen} from "@testing-library/angular";
import {DuplicateDirective} from "./duplicate.directive";

const renderOptions: RenderComponentOptions<unknown> = {
imports: [DuplicateDirective]
};

describe('Duplicate Directive', () => {

it.each([1, 2, 3, 5, 10, 15])(`should render element %s times`, async (duplicateValue) => {
await render(`<p *ngtDuplicate="${duplicateValue}" data-testid="test"></p>`, renderOptions)
const elements = screen.getAllByTestId<HTMLParagraphElement>('test');
expect(elements.length).toEqual(duplicateValue);
})

it('should return correct context for index', async () => {
await render(`<p *ngtDuplicate="5; let index = index" data-testid="test">{{ index }}</p>`, renderOptions);
const elements = screen.getAllByTestId<HTMLParagraphElement>('test');

elements.forEach((element, index) => expect(Number(element.textContent)).toEqual(index));
})

it('should return correct context for first', async () => {
await render(`<p *ngtDuplicate="5; let first = first" data-testid="test">{{ first }}</p>`, renderOptions);
const elements = screen.getAllByTestId<HTMLParagraphElement>('test');

elements.forEach((element, index) => {
const booleanValue = element.textContent === 'true';
const isFirst = index === 0;
expect(booleanValue).toEqual(isFirst);
});
})

it('should return correct context for last', async () => {
await render(`<p *ngtDuplicate="5; let last = last" data-testid="test">{{ last }}</p>`, renderOptions);
const elements = screen.getAllByTestId<HTMLParagraphElement>('test');

elements.forEach((element, index) => {
const booleanValue = element.textContent === 'true';
const isLast = index === elements.length - 1;
expect(booleanValue).toEqual(isLast);
});
})

it('should return correct context for even', async () => {
await render(`<p *ngtDuplicate="5; let even = even" data-testid="test">{{ even }}</p>`, renderOptions);
const elements = screen.getAllByTestId<HTMLParagraphElement>('test');

elements.forEach((element, index) => {
const booleanValue = element.textContent === 'true';
const isEven = index % 2 === 0;
expect(booleanValue).toEqual(isEven);
});
})

it('should return correct context for odd', async () => {
await render(`<p *ngtDuplicate="5; let odd = odd" data-testid="test">{{ odd }}</p>`, renderOptions);
const elements = screen.getAllByTestId<HTMLParagraphElement>('test');

elements.forEach((element, index) => {
const booleanValue = element.textContent === 'true';
const isOdd = index % 2 !== 0;
expect(booleanValue).toEqual(isOdd);
});
})

})
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {Directive, Input, TemplateRef, ViewContainerRef} from "@angular/core";

class DuplicateContext {
constructor(public index: number, public count: number) {}

get first() {
return this.index === 0;
}

get last() {
return this.index === this.count - 1;
}

get even() {
return this.index % 2 === 0;
}

get odd() {
return !this.even;
}
}

@Directive({
selector: '[ngtDuplicate]',
standalone: true
})
export class DuplicateDirective {
constructor(
private templateRef: TemplateRef<DuplicateContext>,
private viewContainerRef: ViewContainerRef
) {
}

@Input('ngtDuplicate') set count(value: number) {
this.viewContainerRef.clear();
for (let index = 0; index < value; index++) {
const context = new DuplicateContext(index, value);
this.viewContainerRef.createEmbeddedView(this.templateRef, context);
}
}

static ngTemplateContextGuard(
directive: DuplicateDirective,
context: DuplicateContext
): context is DuplicateContext {
return true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './duplicate.directive';
1 change: 1 addition & 0 deletions apps/www/src/app/library-components/directives/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './duplicate';
export * from './radio-control-accessor';
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Component, DebugElement, HostBinding, HostListener, Input} from "@angular/core";
import {Component, HostBinding, HostListener, Input} from "@angular/core";
import {RadioValueAccessorDirective} from "./radio-value-accessor.directive";
import {CheckboxControlValueAccessor, FormControl, ReactiveFormsModule} from "@angular/forms";
import {FormControl, ReactiveFormsModule} from "@angular/forms";
import {fireEvent, render, RenderResult} from "@testing-library/angular";

const selectors = {
Expand Down