Skip to content

Commit 44b67a6

Browse files
authored
fix: wait for css files to load before returning the svelte instance (#253)
* wait for css files to load before returning the svelte instance fixes #250 * add test that asserts my change works
1 parent 71766e8 commit 44b67a6

File tree

2 files changed

+40
-25
lines changed

2 files changed

+40
-25
lines changed

cypress/components/styles/styles-spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,19 @@ describe('Styles', () => {
2828
})
2929
cy.get('body').should('have.css', 'background-color', 'rgb(0, 255, 255)')
3030
})
31+
32+
it('adds CSS file and resolves a svelte component', () => {
33+
// Regression test for https://github.com/bahmutov/cypress-svelte-unit-test/issues/250.
34+
// Basically, `mount` was not returning the promise that returns the svelte component instance if using `cssFile` option.
35+
// So the test couldn't access the instance to call svelte's `$set` method, for instance.
36+
mount(Component, null, {
37+
cssFile: 'cypress/components/styles/app.css',
38+
}).then((componentInstance) => {
39+
expect(componentInstance.$set != null).to.eq(
40+
true,
41+
'The resolved object should be a svelte component instance.',
42+
)
43+
})
44+
cy.get('body').should('have.css', 'background-color', 'rgb(0, 255, 255)')
45+
})
3146
})

lib/index.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -113,36 +113,36 @@ export function mount(
113113
el.setAttribute('id', rootId)
114114
document.getElementsByTagName('body')[0].prepend(el)
115115
}
116-
injectStylesBeforeElement(styleOptions, document, el)
116+
return injectStylesBeforeElement(styleOptions, document, el).then(() => {
117+
// by default we mount the component into the created element
118+
let target = el
117119

118-
// by default we mount the component into the created element
119-
let target = el
120-
121-
if (styleOptions && styleOptions.html) {
122-
el.innerHTML = styleOptions.html
123-
target = document.getElementById('here')
124-
if (!target) {
125-
console.error('mount has HTML with DIV with ID "here"')
126-
console.error(styleOptions.html)
127-
throw new Error(
128-
'Could not find element with ID "here" in the HTML passed',
129-
)
120+
if (styleOptions && styleOptions.html) {
121+
el.innerHTML = styleOptions.html
122+
target = document.getElementById('here')
123+
if (!target) {
124+
console.error('mount has HTML with DIV with ID "here"')
125+
console.error(styleOptions.html)
126+
throw new Error(
127+
'Could not find element with ID "here" in the HTML passed',
128+
)
129+
}
130130
}
131-
}
132-
133-
const allOptions = Object.assign({}, options, {
134-
target,
135-
})
136131

137-
const component = new Component(allOptions)
138-
if (options.callbacks) {
139-
// write message callbacks
140-
Object.keys(options.callbacks).forEach((message) => {
141-
component.$$.callbacks[message] = [options.callbacks![message]]
132+
const allOptions = Object.assign({}, options, {
133+
target,
142134
})
143-
}
144135

145-
return cy.wrap(component)
136+
const component = new Component(allOptions)
137+
if (options.callbacks) {
138+
// write message callbacks
139+
Object.keys(options.callbacks).forEach((message) => {
140+
component.$$.callbacks[message] = [options.callbacks![message]]
141+
})
142+
}
143+
144+
return cy.wrap(component)
145+
})
146146
})
147147
}
148148

0 commit comments

Comments
 (0)