|
| 1 | +import {render, RenderComponentOptions, screen} from "@testing-library/angular"; |
| 2 | +import {DuplicateDirective} from "./duplicate.directive"; |
| 3 | + |
| 4 | +const renderOptions: RenderComponentOptions<unknown> = { |
| 5 | + imports: [DuplicateDirective] |
| 6 | +}; |
| 7 | + |
| 8 | +describe('Duplicate Directive', () => { |
| 9 | + |
| 10 | + it.each([1, 2, 3, 5, 10, 15])(`should render element %s times`, async (duplicateValue) => { |
| 11 | + await render(`<p *ngtDuplicate="${duplicateValue}" data-testid="test"></p>`, renderOptions) |
| 12 | + const elements = screen.getAllByTestId<HTMLParagraphElement>('test'); |
| 13 | + expect(elements.length).toEqual(duplicateValue); |
| 14 | + }) |
| 15 | + |
| 16 | + it('should return correct context for index', async () => { |
| 17 | + await render(`<p *ngtDuplicate="5; let index = index" data-testid="test">{{ index }}</p>`, renderOptions); |
| 18 | + const elements = screen.getAllByTestId<HTMLParagraphElement>('test'); |
| 19 | + |
| 20 | + elements.forEach((element, index) => expect(Number(element.textContent)).toEqual(index)); |
| 21 | + }) |
| 22 | + |
| 23 | + it('should return correct context for first', async () => { |
| 24 | + await render(`<p *ngtDuplicate="5; let first = first" data-testid="test">{{ first }}</p>`, renderOptions); |
| 25 | + const elements = screen.getAllByTestId<HTMLParagraphElement>('test'); |
| 26 | + |
| 27 | + elements.forEach((element, index) => { |
| 28 | + const booleanValue = element.textContent === 'true'; |
| 29 | + const isFirst = index === 0; |
| 30 | + expect(booleanValue).toEqual(isFirst); |
| 31 | + }); |
| 32 | + }) |
| 33 | + |
| 34 | + it('should return correct context for last', async () => { |
| 35 | + await render(`<p *ngtDuplicate="5; let last = last" data-testid="test">{{ last }}</p>`, renderOptions); |
| 36 | + const elements = screen.getAllByTestId<HTMLParagraphElement>('test'); |
| 37 | + |
| 38 | + elements.forEach((element, index) => { |
| 39 | + const booleanValue = element.textContent === 'true'; |
| 40 | + const isLast = index === elements.length - 1; |
| 41 | + expect(booleanValue).toEqual(isLast); |
| 42 | + }); |
| 43 | + }) |
| 44 | + |
| 45 | + it('should return correct context for even', async () => { |
| 46 | + await render(`<p *ngtDuplicate="5; let even = even" data-testid="test">{{ even }}</p>`, renderOptions); |
| 47 | + const elements = screen.getAllByTestId<HTMLParagraphElement>('test'); |
| 48 | + |
| 49 | + elements.forEach((element, index) => { |
| 50 | + const booleanValue = element.textContent === 'true'; |
| 51 | + const isEven = index % 2 === 0; |
| 52 | + expect(booleanValue).toEqual(isEven); |
| 53 | + }); |
| 54 | + }) |
| 55 | + |
| 56 | + it('should return correct context for odd', async () => { |
| 57 | + await render(`<p *ngtDuplicate="5; let odd = odd" data-testid="test">{{ odd }}</p>`, renderOptions); |
| 58 | + const elements = screen.getAllByTestId<HTMLParagraphElement>('test'); |
| 59 | + |
| 60 | + elements.forEach((element, index) => { |
| 61 | + const booleanValue = element.textContent === 'true'; |
| 62 | + const isOdd = index % 2 !== 0; |
| 63 | + expect(booleanValue).toEqual(isOdd); |
| 64 | + }); |
| 65 | + }) |
| 66 | + |
| 67 | +}) |
0 commit comments