|
1 | 1 | import './setup'; |
2 | | -import { StageComponent } from 'aurelia-testing'; |
| 2 | +import * as AuBinding from 'aurelia-binding'; |
| 3 | +import { StageComponent, ComponentTester } from 'aurelia-testing'; |
3 | 4 | import { bootstrap } from 'aurelia-bootstrapper'; |
4 | 5 | import { waitForFrames } from './test-utilities'; |
5 | 6 | import { Repeat } from '../src/repeat'; |
6 | 7 |
|
7 | 8 | // https://github.com/aurelia/templating-resources/issues/378 |
8 | 9 | describe('repeat.issue-378.spec.ts', () => { |
| 10 | + it('extracts matcher binding corectly when matcher is on repeated element', async () => { |
| 11 | + const model = new class { |
| 12 | + products = [ |
| 13 | + { id: 0, name: 'Motherboard' }, |
| 14 | + { id: 1, name: 'CPU' }, |
| 15 | + { id: 2, name: 'Memory' } |
| 16 | + ]; |
| 17 | + |
| 18 | + productMatcher = (a, b) => { |
| 19 | + return a.id === b.id; |
| 20 | + } |
| 21 | + |
| 22 | + selectedProduct = { id: 1, name: 'CPU' }; |
| 23 | + }; |
| 24 | + const component: ComponentTester<Repeat> = StageComponent |
| 25 | + .withResources() |
| 26 | + .inView(`<label repeat.for="product of products" matcher.bind="productMatcher"> |
| 27 | + \${product.id} - \${product.name} |
| 28 | + </label>`) |
| 29 | + .boundTo(model); |
| 30 | + |
| 31 | + await component.create(bootstrap); |
| 32 | + |
| 33 | + const matcherBinding = component.viewModel.matcherBinding; |
| 34 | + expect(matcherBinding).not.toBe(undefined, 'matcher binding should have existed'); |
| 35 | + expect(matcherBinding instanceof (AuBinding as any).BindingExpression).toBe(true, 'it should have been a binding expression'); |
| 36 | + |
| 37 | + // verify that it leaves attribute intact |
| 38 | + // even when binding expression is extracted |
| 39 | + // todo: should it remove it? |
| 40 | + expect( |
| 41 | + Array.from(component['host'].querySelectorAll('label')) |
| 42 | + .filter((label: HTMLElement) => label.hasAttribute('matcher.bind')).length).toBe(3); |
| 43 | + }); |
| 44 | + |
| 45 | + it('does not extracts matcher binding when matcher is on repeated <template/> element', async () => { |
| 46 | + const model = new class { |
| 47 | + products = [ |
| 48 | + { id: 0, name: 'Motherboard' }, |
| 49 | + { id: 1, name: 'CPU' }, |
| 50 | + { id: 2, name: 'Memory' } |
| 51 | + ]; |
| 52 | + |
| 53 | + productMatcher = (a, b) => { |
| 54 | + return a.id === b.id; |
| 55 | + } |
| 56 | + |
| 57 | + selectedProduct = { id: 1, name: 'CPU' }; |
| 58 | + }; |
| 59 | + const component: ComponentTester<Repeat> = StageComponent |
| 60 | + .withResources() |
| 61 | + .inView(`<template repeat.for="product of products" matcher.bind="productMatcher"> |
| 62 | + <span>\${product.id} - \${product.name}</span> |
| 63 | + </template>`) |
| 64 | + .boundTo(model); |
| 65 | + |
| 66 | + await component.create(bootstrap); |
| 67 | + |
| 68 | + const matcherBinding = component.viewModel.matcherBinding; |
| 69 | + expect(matcherBinding).toBe(undefined, 'matcher binding should have existed'); |
| 70 | + |
| 71 | + }); |
| 72 | + |
9 | 73 | it('works with <div repeat[Array] /> -->> <... matcher />', async () => { |
10 | 74 | Repeat.useInnerMatcher = false; |
11 | 75 | const model = new class { |
|
0 commit comments