Skip to content

Commit

Permalink
fix(pencil): Attribute support
Browse files Browse the repository at this point in the history
  • Loading branch information
larowlan committed Apr 20, 2021
1 parent 2460dac commit bb75fdd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ const loadTemplate = async (file, context = {}, namespaces) => {
return Twig.twigAsync({
path: file,
}).then((template) => {
context.attributes = new DrupalAttribute()
if (!context.hasOwnProperty("attributes")) {
context.attributes = new DrupalAttribute()
}
return template.render(context)
})
}
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/accordion.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<details class="accordion" {% if open %}open{% endif %}>
<details {{ attributes.addClass('accordion') }} {% if open %}open{% endif %}>
<summary class="accordion__title">{{ title|default('Accordion title')|backwords }}</summary>
<div class="accordion__content">
{% block accordion_content %}
Expand Down
16 changes: 16 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint no-new: 0 */
import Accordion from './fixtures/accordion';
import {render, fireEvent, Twig} from "../src";
import DrupalAttribute from "drupal-attribute"

Twig.extendFilter("backwords", (text) => {
return text.split(" ").reverse().join(" ");
Expand Down Expand Up @@ -58,4 +59,19 @@ describe('Test library by testing an accordion', () => {
expect(summaryElement.getAttribute('aria-expanded')).toEqual('false');
expect(summaryElement.getAttribute('aria-pressed')).toEqual('false');
});

it('Can support passing attributes', async () => {
const attributes = new DrupalAttribute()
attributes.set('data-foo', 'bar')
const { container, getByText } = await render('./tests/fixtures/accordion.twig', {
// This is intentionally backwards so we can test extending twig.
title: 'title Accordion',
open: false,
attributes
}, {
'twig-testing-library-tests': './tests/fixtures/'
});
const accordionElement = container.querySelector('.accordion');
expect(accordionElement.dataset.foo).toEqual('bar');
});
});

0 comments on commit bb75fdd

Please sign in to comment.