|
| 1 | +import { expect } from '@esm-bundle/chai'; |
| 2 | +import { fixtureSync } from '@vaadin/testing-helpers'; |
| 3 | +import { PolymerElement, html } from '@polymer/polymer/polymer-element.js'; |
| 4 | +import { SlotMixin } from '../src/slot-mixin.js'; |
| 5 | +import { SlotStylesMixin } from '../src/slot-styles-mixin.js'; |
| 6 | + |
| 7 | +describe('slot-styles-mixin', () => { |
| 8 | + const COLOR = 'rgb(0, 100, 0)'; |
| 9 | + let wrapper, element, button; |
| 10 | + |
| 11 | + before(() => { |
| 12 | + customElements.define( |
| 13 | + 'slot-styles-mixin-element', |
| 14 | + class extends SlotStylesMixin(SlotMixin(PolymerElement)) { |
| 15 | + static get template() { |
| 16 | + return html`<slot name="button"></slot>`; |
| 17 | + } |
| 18 | + |
| 19 | + get slots() { |
| 20 | + return { |
| 21 | + button: () => { |
| 22 | + const button = document.createElement('button'); |
| 23 | + button.textContent = 'Button'; |
| 24 | + return button; |
| 25 | + } |
| 26 | + }; |
| 27 | + } |
| 28 | + |
| 29 | + get slotStyles() { |
| 30 | + return [ |
| 31 | + ` |
| 32 | + button[slot='button'] { |
| 33 | + color: ${COLOR}; |
| 34 | + } |
| 35 | + ` |
| 36 | + ]; |
| 37 | + } |
| 38 | + } |
| 39 | + ); |
| 40 | + }); |
| 41 | + |
| 42 | + beforeEach(() => { |
| 43 | + wrapper = fixtureSync('<div></div'); |
| 44 | + wrapper.attachShadow({ mode: 'open' }); |
| 45 | + element = document.createElement('slot-styles-mixin-element'); |
| 46 | + wrapper.shadowRoot.appendChild(element); |
| 47 | + button = element.querySelector('button'); |
| 48 | + }); |
| 49 | + |
| 50 | + afterEach(() => { |
| 51 | + element.remove(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should append styles to wrapper shadow root', () => { |
| 55 | + expect(getComputedStyle(button).color).to.equal(COLOR); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should only append styles with same ID once', () => { |
| 59 | + const sibling = document.createElement('slot-styles-mixin-element'); |
| 60 | + element.parentNode.appendChild(sibling); |
| 61 | + expect(wrapper.shadowRoot.querySelectorAll('style').length).to.equal(1); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should append styles when moved to other shadow root', () => { |
| 65 | + const inner = document.createElement('div'); |
| 66 | + inner.attachShadow({ mode: 'open' }); |
| 67 | + wrapper.shadowRoot.appendChild(inner); |
| 68 | + // move to inner shadow root |
| 69 | + inner.shadowRoot.appendChild(element); |
| 70 | + expect(getComputedStyle(button).color).to.equal(COLOR); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should append styles when moved to document body', () => { |
| 74 | + // move out of shadow root |
| 75 | + wrapper.appendChild(element); |
| 76 | + expect(getComputedStyle(button).color).to.equal(COLOR); |
| 77 | + }); |
| 78 | +}); |
0 commit comments