Skip to content
This repository was archived by the owner on Dec 12, 2020. It is now read-only.

Fix Vuex integration and add Counter example tests #13

Merged
merged 4 commits into from
Jan 28, 2018

Conversation

amirrustam
Copy link
Contributor

Current fix for #6

The inner Vue instance within Vuex Store must be refeshed by `resetStoreVM` to restore reactivity of the store state. This doesn’t fix stale mapped getters within components. That’s a separate WIP issue.
Current mapped vuex getters within components become stale. To have a working Counter example, the computed mapped `evenOrOdd` is accessed directly via `$store`. This will be changed back once the stale mapped getter issue (WIP) is fixed.
Added tests to cover all the features within the component. Fixes cypress-io#6
@bahmutov
Copy link
Contributor

bahmutov commented Jan 26, 2018 via email

@amirrustam
Copy link
Contributor Author

amirrustam commented Jan 26, 2018

@bahmutov I've made some great progress on this. The Vuex store not being reactive had to do with how Vuex manages its internal state.

When creating a store with Vuex.Store({ ... }), a Vue instance (_vm) is initialized within the Store instance to make its internal state reactive:

// vuex/src/store.js
store._vm = new Vue({
    data: {
      $$state: state
    },
    computed
  })

We create our Store instance with a different Vue "bundle" than the one we include with a script tag within the component iframe. This essentially makes that inner Vue instance go stale.

The inner state will still change after button clicks that trigger mutations, but the watchers that look for state changes to add DOM manipulations to Vue's queueing system no longer work in this scenario.

So we need to use the global Vue within the component iframe to initialize the Store's inner Vue instance. When we run mountVue, we check for a store within the provided component, if it has one, then we reset this store's _vm with the Vue on the iframe window. This fixes the reactivity problem.

I've added tests for all the buttons within the Counter component:

vuex counter unit test

There is still one last lingering issue (amirrustam#1), and the computed mapped getters on a component can go stale. So in templates instead of using {{ evenOrOdd }}, I have to do {{ $store.getters.evenOrOdd }}, because the latest value of evenOrOdd is not always reflected in its computed prop. Obviously this is not ideal, and I'm working on fixing this. This was just my first stab at the issue, and I have a couple of hunches!

I want Vue.js users to have a great experience with Cypress, and to use Cypress for all their tests. So I want to improve component unit-testing for them as much as possible.

@bahmutov
Copy link
Contributor

I will merge this in - I was thinking about the global flag ... is this related to the two iframes? Maybe we can reflect the property from app iframe back into spec iframe to solve this.

@bahmutov bahmutov merged commit 3e1cfeb into cypress-io:vuex-example-6 Jan 28, 2018
@bahmutov
Copy link
Contributor

I merged this because I think this improves the total experience, thanks a log for great work @amirrustam

@amirrustam
Copy link
Contributor Author

@bahmutov we think the same. I did something similar earlier today, and it got everything fully working. I was going to commit and push it when I get home. Await my next pull request :)

amirrustam added a commit to amirrustam/cypress-vue-unit-test that referenced this pull request Jan 28, 2018
Refactors how the Vue instance within the component app frame is initialized, and fully fixes Vue integration.
- fixes lingering issues after pull request cypress-io#13
- fully fixes cypress-io#6
- fixes #1
- README docs have been updated to reflect refactoring
- 'vue' option has been deprecated (with warning to user) as it's no longer necessary
- spread operator support has been added for upcoming Vuex example
amirrustam added a commit to amirrustam/cypress-vue-unit-test that referenced this pull request Jan 28, 2018
To thoroughly demonstrate Vuex support after the refactoring in commit e8773383, Counter.vue now:
- Utilizes all Vuex mapping functions.
- Can properly use computed mapped getters in its template. This was an issue after cypress-io#13.
- Set count state via an input field to demonstrate mapped mutations. Two tests have been added to the spec for this new input field.

Close cypress-io#6
bahmutov pushed a commit that referenced this pull request Jan 31, 2018
* fix: vuex integration (#6)

The inner Vue instance within Vuex Store must be refeshed by `resetStoreVM` to restore reactivity of the store state. This doesn’t fix stale mapped getters within components. That’s a separate WIP issue.

* fix: stale mapped getter `evenOrOdd` (#6)

Current mapped vuex getters within components become stale. To have a working Counter example, the computed mapped `evenOrOdd` is accessed directly via `$store`. This will be changed back once the stale mapped getter issue (WIP) is fixed.

* feat: added example tests for Counter component

Added tests to cover all the features within the component. Fixes #6

* minor: following better practices

* fix: refactor Vue initialization in app frame

Refactors how the Vue instance within the component app frame is initialized, and fully fixes Vue integration.
- fixes lingering issues after pull request #13
- fully fixes #6
- fixes amirrustam#1
- README docs have been updated to reflect refactoring
- 'vue' option has been deprecated (with warning to user) as it's no longer necessary
- spread operator support has been added for upcoming Vuex example

* feat: enhanced Vuex Counter example and test spec

To thoroughly demonstrate Vuex support after the refactoring in commit amirrustam/cypress-vue-unit-test@e8773383, Counter.vue now:
- Utilizes all Vuex mapping functions.
- Can properly use computed mapped getters in its template. This was an issue after #13.
- Set count state via an input field to demonstrate mapped mutations. Two tests have been added to the spec for this new input field.

Close #6

* minor: remove redundant declaration
bahmutov added a commit that referenced this pull request Jan 31, 2018
* working on Vuex example for #6

* fix: Vuex integration and add Counter example tests (#13)

* fix: vuex integration (#6)

The inner Vue instance within Vuex Store must be refeshed by `resetStoreVM` to restore reactivity of the store state. This doesn’t fix stale mapped getters within components. That’s a separate WIP issue.

* fix: stale mapped getter `evenOrOdd` (#6)

Current mapped vuex getters within components become stale. To have a working Counter example, the computed mapped `evenOrOdd` is accessed directly via `$store`. This will be changed back once the stale mapped getter issue (WIP) is fixed.

* feat: added example tests for Counter component

Added tests to cover all the features within the component. Fixes #6

* minor: following better practices

* Full Vuex support with enhanced Counter.vue example and test spec (#15)

* fix: vuex integration (#6)

The inner Vue instance within Vuex Store must be refeshed by `resetStoreVM` to restore reactivity of the store state. This doesn’t fix stale mapped getters within components. That’s a separate WIP issue.

* fix: stale mapped getter `evenOrOdd` (#6)

Current mapped vuex getters within components become stale. To have a working Counter example, the computed mapped `evenOrOdd` is accessed directly via `$store`. This will be changed back once the stale mapped getter issue (WIP) is fixed.

* feat: added example tests for Counter component

Added tests to cover all the features within the component. Fixes #6

* minor: following better practices

* fix: refactor Vue initialization in app frame

Refactors how the Vue instance within the component app frame is initialized, and fully fixes Vue integration.
- fixes lingering issues after pull request #13
- fully fixes #6
- fixes amirrustam#1
- README docs have been updated to reflect refactoring
- 'vue' option has been deprecated (with warning to user) as it's no longer necessary
- spread operator support has been added for upcoming Vuex example

* feat: enhanced Vuex Counter example and test spec

To thoroughly demonstrate Vuex support after the refactoring in commit amirrustam/cypress-vue-unit-test@e8773383, Counter.vue now:
- Utilizes all Vuex mapping functions.
- Can properly use computed mapped getters in its template. This was an issue after #13.
- Set count state via an input field to demonstrate mapped mutations. Two tests have been added to the spec for this new input field.

Close #6

* minor: remove redundant declaration
JessicaSachs pushed a commit to cypress-io/cypress that referenced this pull request Sep 29, 2020
* working on Vuex example for cypress-io/cypress-vue-unit-test#6

* fix: Vuex integration and add Counter example tests (cypress-io/cypress-vue-unit-test#13)

* fix: vuex integration (bahmutov/cypress-vue-unit-testbahmutov/cypress-vue-unit-test#6)

The inner Vue instance within Vuex Store must be refeshed by `resetStoreVM` to restore reactivity of the store state. This doesn’t fix stale mapped getters within components. That’s a separate WIP issue.

* fix: stale mapped getter `evenOrOdd` (bahmutov/cypress-vue-unit-testbahmutov/cypress-vue-unit-test#6)

Current mapped vuex getters within components become stale. To have a working Counter example, the computed mapped `evenOrOdd` is accessed directly via `$store`. This will be changed back once the stale mapped getter issue (WIP) is fixed.

* feat: added example tests for Counter component

Added tests to cover all the features within the component. Fixes bahmutov/cypress-vue-unit-testbahmutov/cypress-vue-unit-test#6

* minor: following better practices

* Full Vuex support with enhanced Counter.vue example and test spec (cypress-io/cypress-vue-unit-test#15)

* fix: vuex integration (bahmutov/cypress-vue-unit-testbahmutov/cypress-vue-unit-test#6)

The inner Vue instance within Vuex Store must be refeshed by `resetStoreVM` to restore reactivity of the store state. This doesn’t fix stale mapped getters within components. That’s a separate WIP issue.

* fix: stale mapped getter `evenOrOdd` (bahmutov/cypress-vue-unit-testbahmutov/cypress-vue-unit-test#6)

Current mapped vuex getters within components become stale. To have a working Counter example, the computed mapped `evenOrOdd` is accessed directly via `$store`. This will be changed back once the stale mapped getter issue (WIP) is fixed.

* feat: added example tests for Counter component

Added tests to cover all the features within the component. Fixes bahmutov/cypress-vue-unit-testbahmutov/cypress-vue-unit-test#6

* minor: following better practices

* fix: refactor Vue initialization in app frame

Refactors how the Vue instance within the component app frame is initialized, and fully fixes Vue integration.
- fixes lingering issues after pull request bahmutov/cypress-vue-unit-testbahmutov/cypress-vue-unit-test#13
- fully fixes bahmutov/cypress-vue-unit-testbahmutov/cypress-vue-unit-test#6
- fixes amirrustam/cypress-vue-unit-testbahmutov/cypress-vue-unit-test#1
- README docs have been updated to reflect refactoring
- 'vue' option has been deprecated (with warning to user) as it's no longer necessary
- spread operator support has been added for upcoming Vuex example

* feat: enhanced Vuex Counter example and test spec

To thoroughly demonstrate Vuex support after the refactoring in commit amirrustam/cypress-vue-unit-test@e8773383, Counter.vue now:
- Utilizes all Vuex mapping functions.
- Can properly use computed mapped getters in its template. This was an issue after bahmutov/cypress-vue-unit-testbahmutov/cypress-vue-unit-test#13.
- Set count state via an input field to demonstrate mapped mutations. Two tests have been added to the spec for this new input field.

Close bahmutov/cypress-vue-unit-testbahmutov/cypress-vue-unit-test#6

* minor: remove redundant declaration
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants