Skip to content

Commit

Permalink
assertVue*() methods support Vue 3 (#834)
Browse files Browse the repository at this point in the history
Vue 3 no longer adds a __vue__ property to
DOM nodes to allow querying for props. Instead
the __vueParentComponent must be inspected.
Its 'ctx' property will return the:

* passed in component props
* data()
* computed props

When Dusk is looking up the Vue prop value
of a DOM node, it will first look for a Vue 2
__vue__ property, then fallback to
__vueParentComponent.ctx for Vue 3.
  • Loading branch information
derekmd authored Nov 20, 2020
1 parent 2944d01 commit 601652e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Concerns/MakesAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,10 @@ public function vueAttribute($componentSelector, $key)
$fullSelector = $this->resolver->format($componentSelector);

return $this->driver->executeScript(
"return document.querySelector('".$fullSelector."').__vue__.".$key
"var el = document.querySelector('".$fullSelector."');".
"return typeof el.__vue__ === 'undefined' ".
'? JSON.parse(JSON.stringify(el.__vueParentComponent.ctx)).'.$key.
': el.__vue__.'.$key
);
}

Expand Down
18 changes: 18 additions & 0 deletions tests/MakesAssertionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,24 @@ public function test_assert_selected()
}
}

public function test_assert_vue_contains_formats_vue_prop_query()
{
$driver = m::mock(stdClass::class);
$driver->shouldReceive('executeScript')
->with(
'var el = document.querySelector(\'body [dusk="vue-component"]\');'.
"return typeof el.__vue__ === 'undefined' ".
'? JSON.parse(JSON.stringify(el.__vueParentComponent.ctx)).name'.
': el.__vue__.name'
)
->once()
->andReturn(['john']);

$browser = new Browser($driver);

$browser->assertVueContains('name', 'john', '@vue-component');
}

public function test_assert_vue_contains()
{
$driver = m::mock(stdClass::class);
Expand Down

0 comments on commit 601652e

Please sign in to comment.