Skip to content

Commit

Permalink
[FIX] Move into async func
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Brazeilles committed Oct 4, 2024
1 parent 08e12ad commit e457883
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 70 deletions.
80 changes: 43 additions & 37 deletions packages/mjml/test/html-attributes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const { load } = require('cheerio')
const { sortBy } = require('lodash')
const mjml = require('../lib')

const input = `
async function run() {
const input = `
<mjml>
<mj-head>
<mj-html-attributes>
Expand Down Expand Up @@ -35,44 +36,49 @@ const input = `
</mjml>
`

const { html } = mjml(input)
const $ = load(html)
const { html } = await mjml(input)
const $ = load(html)

// should put the attributes at the right place
chai
.expect(
$('.text div')
.map(function getAttr() {
return $(this).attr('data-id')
})
.get(),
'Custom attributes added on texts',
)
.to.eql(['42', '42'])
// should put the attributes at the right place
chai
.expect(
$('.text div')
.map(function getAttr() {
return $(this).attr('data-id')
})
.get(),
'Custom attributes added on texts',
)
.to.eql(['42', '42'])

chai
.expect(
$('.image td')
.map(function getAttr() {
return $(this).attr('data-name')
})
.get(),
'Custom attributes added on image',
)
.to.eql(['43'])
chai
.expect(
$('.image td')
.map(function getAttr() {
return $(this).attr('data-name')
})
.get(),
'Custom attributes added on image',
)
.to.eql(['43'])

// should not alter templating syntax, or move the content that is outside any tag (mj-raws)
const expected = [
'{ if item < 5 }',
'class="section"',
'{ if item > 10 }',
'class="text"',
'{ item }',
'{ end if }',
'{ item + 1 }',
]
const indexes = expected.map((str) => html.indexOf(str))
// should not alter templating syntax, or move the content that is outside any tag (mj-raws)
const expected = [
'{ if item < 5 }',
'class="section"',
'{ if item > 10 }',
'class="text"',
'{ item }',
'{ end if }',
'{ item + 1 }',
]
const indexes = expected.map((str) => html.indexOf(str))

chai.expect(indexes, 'Templating syntax unaltered').to.not.include(-1)
chai.expect(indexes, 'Templating syntax unaltered').to.not.include(-1)

chai.expect(sortBy(indexes), 'Mj-raws kept same positions').to.deep.eql(indexes)
chai
.expect(sortBy(indexes), 'Mj-raws kept same positions')
.to.deep.eql(indexes)
}

run()
69 changes: 36 additions & 33 deletions packages/mjml/test/lazy-head-style.test.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,49 @@
const chai = require('chai')
const spies = require('chai-spies')
const mjml = require('../lib')

chai.use(spies)

const {
HeadComponent,
registerComponent,
} = require('../../mjml-core/lib/index')

const addStyle = chai.spy(
(breakpoint) => `
@media only screen and (max-width:${breakpoint}) {
h1 {
font-size: 20px;
async function run() {
chai.use(spies)

const addStyle = chai.spy(
(breakpoint) => `
@media only screen and (max-width:${breakpoint}) {
h1 {
font-size: 20px;
}
}
}
`,
)
`,
)

class HeadComponentWithFunctionStyle extends HeadComponent {
handler() {
const { add } = this.context
add('style', addStyle)
class HeadComponentWithFunctionStyle extends HeadComponent {
handler() {
const { add } = this.context
add('style', addStyle)
}
}
HeadComponentWithFunctionStyle.componentName =
'mj-head-component-with-function-style'
HeadComponentWithFunctionStyle.endingTag = true
HeadComponentWithFunctionStyle.allowedAttributes = {}

registerComponent(HeadComponentWithFunctionStyle)

mjml(`
<mjml>
<mj-head>
<mj-head-component-with-function-style />
<mj-breakpoint width="300px" />
</mj-head>
<mj-body>
</mj-body>
</mjml>
`)

chai.expect(addStyle).to.have.been.called.with('300px')
}
HeadComponentWithFunctionStyle.componentName = 'mj-head-component-with-function-style'
HeadComponentWithFunctionStyle.endingTag = true
HeadComponentWithFunctionStyle.allowedAttributes = {}


registerComponent(HeadComponentWithFunctionStyle)

mjml(`
<mjml>
<mj-head>
<mj-head-component-with-function-style />
<mj-breakpoint width="300px" />
</mj-head>
<mj-body>
</mj-body>
</mjml>
`)

chai.expect(addStyle).to.have.been.called.with('300px')
run()

0 comments on commit e457883

Please sign in to comment.