Skip to content

Commit

Permalink
fix: state not syncing in iframe mode, fix #361
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Dec 3, 2022
1 parent 16a13af commit 4b7fe0c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
49 changes: 49 additions & 0 deletions examples/vue3/cypress/integration/state.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/// <reference types="cypress" />

describe('State Options API', () => {
const getIframeBody = () => cy.get('iframe[data-test-id="preview-iframe"]').iframe()

beforeEach(() => {
cy.visit('/story/src-components-stateoption-story-vue?variantId=src-components-stateoption-story-vue-0')
})

it('syncs state', () => {
getIframeBody().find('.state-output').contains('"optionApiData": "OPTION API"')
cy.get('[data-test-id="story-controls"]').get('input[type="text"]').clear().type('Meow')
getIframeBody().find('.state-output').contains('"optionApiData": "Meow"')
})
})

describe('State Setup API', () => {
const getIframeBody = () => cy.get('iframe[data-test-id="preview-iframe"]').iframe()

beforeEach(() => {
cy.visit('/story/src-components-statesetup-story-vue?variantId=src-components-statesetup-story-vue-0')
})

it('syncs state', () => {
getIframeBody().find('pre').contains('"count": 0')
getIframeBody().find('pre').contains('"text": "Meow"')
cy.get('[data-test-id="story-controls"] .controls').contains('+1').click().click()
cy.get('[data-test-id="story-controls"] input[type="text"]').eq(0).clear().type('Waf')
getIframeBody().find('pre').contains('"count": 2')
getIframeBody().find('pre').contains('"text": "Waf"')
})
})

describe('State Setup API (2)', () => {
const getIframeBody = () => cy.get('iframe[data-test-id="preview-iframe"]').iframe()

beforeEach(() => {
cy.visit('/story/src-components-statesetup2-story-vue?variantId=src-components-statesetup2-story-vue-0')
})

it('syncs state', () => {
getIframeBody().find('pre').contains('"count": 0')
getIframeBody().find('pre').contains('"text": "Meow"')
cy.get('[data-test-id="story-controls"] .controls').contains('+1').click().click()
cy.get('[data-test-id="story-controls"] input[type="text"]').eq(0).clear().type('Waf')
getIframeBody().find('pre').contains('"count": 2')
getIframeBody().find('pre').contains('"text": "Waf"')
})
})
2 changes: 1 addition & 1 deletion examples/vue3/src/components/StateOption.story.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default {
<Variant>
<h1>State</h1>
<div>
<pre>{{ { optionApiData } }}</pre>
<pre class="state-output">{{ { optionApiData } }}</pre>
<input
v-model="optionApiData"
>
Expand Down
9 changes: 7 additions & 2 deletions packages/histoire-shared/src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ export function omit (data, keys: string[]) {
return copy
}

export function applyState (target: any, state: any) {
export function applyState (target: any, state: any, override = false) {
for (const key in state) {
target[key] = state[key]
// iframe sync needs to update properties without overriding them
if (!override && target[key] && !key.startsWith('_h') && typeof target[key] === 'object' && !Array.isArray(target[key])) {
Object.assign(target[key], state[key])
} else {
target[key] = state[key]
}
}
}

0 comments on commit 4b7fe0c

Please sign in to comment.