Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Add Cypress test - New Actions Rendering #1672

Merged
merged 4 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion .github/workflows/cypress.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
name: Cypress

on:
workflow_dispatch:
push:
branches:
- "**"
paths-ignore:
- "art/**"
- "README.md"
pull_request:
types: [ready_for_review, synchronize, opened]
paths-ignore:
- "art/**"
- "README.md"

jobs:
build:
Expand Down
2 changes: 0 additions & 2 deletions src/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ final class Button implements Wireable

public array $iconAttributes = [];

public ?\Closure $hideWhen = null;

public bool | \Closure $can = true;

public function __construct(public string $action)
Expand Down
16 changes: 6 additions & 10 deletions src/Providers/Macros.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,12 @@ public static function actions(): void
return $this;
});

Button::macro('disable', function () {
$this->attributes([
'disabled' => 'disabled',
]);
Button::macro('disable', function (bool $disable = true) {
if ($disable) {
$this->attributes([
'disabled' => 'disabled',
]);
}

return $this;
});
Expand Down Expand Up @@ -198,12 +200,6 @@ public static function actions(): void
return $this;
});

Button::macro('hideWhen', function (\Closure $closure) {
$this->hideWhen = $closure;

return $this;
});

Button::macro('can', function (bool|\Closure $closure) {
$this->can = $closure;

Expand Down
159 changes: 159 additions & 0 deletions tests/cypress/cypress/e2e/actions/attributes.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
describe('render javascript actions attributes', () => {
beforeEach(() => {
cy.visit('/actions-attributes');
});

it('can render view button and click for row 1', () => {
cy.get('.power-grid-table tbody tr').eq(0)
.should('contain.html', 'pgRenderActions({ rowId: 1, parentId: null })')
.should('contain.html', 'wire:click="$dispatch(\'clickToEdit\'')
.should('contain.html', 'data-cy="btn-view-1"')
.should('contain.html', 'class="text-slate-500 items-center flex gap-2 hover:text-slate-700 hover:bg-slate-100 p-1 px-2 rounded"')
// Eye icon
.should('contain.html', '<svg class="w-5 text-red-600 !text-green-500" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">\n' +
' <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path>\n' +
' <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"></path>\n' +
'</svg>')
.should('contain.text', 'View')

cy.intercept('POST', '/livewire/update', (req) => {
expect(req.body.components[0].calls[0])
.to.deep
.equal({
path: "",
method: "__dispatch",
params: [
"clickToEdit",
{
action: "view",
name: "Luan"
}
]
});

}).as('requestIntercept');

cy.get('[data-cy=btn-view-1]').click()

cy.wait('@requestIntercept')
.its('response.body')
.should((response) => {
expect(response.components[0].effects.xjs[0])
.to.deep
.equal('console.log("Editing #view - Luan")');
});
});

it('can render "Edit" button and click for row 1', () => {
cy.get('.power-grid-table tbody tr').eq(0)
.should('contain.html', 'pgRenderActions({ rowId: 1, parentId: null })')
.should('contain.html', 'data-cy="btn-edit-1"')
.should('contain.html', 'class="text-slate-500 items-center flex gap-2 hover:text-slate-700 hover:bg-slate-100 p-1 px-2 rounded"')
.should('contain.html', 'wire:click="$dispatch(\'clickToEdit\'')
// Pencil icon
.should('contain.html', '<svg class="w-5 text-red-600 !text-green-500" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">\n' +
' <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z"></path>\n' +
'</svg>')
.should('contain.text', 'Edit')

cy.intercept('POST', '/livewire/update', (req) => {
expect(req.body.components[0].calls[0])
.to.deep
.equal({
path: "",
method: "__dispatch",
params: [
"clickToEdit",
{
action: "edit",
name: "Luan"
}
]
});

}).as('requestIntercept');

cy.get('[data-cy=btn-edit-1]').click()

cy.wait('@requestIntercept')
.its('response.body')
.should((response) => {
expect(response.components[0].effects.xjs[0])
.to.deep
.equal('console.log("Editing #edit - Luan")');
});
});

it('can render view button and click for row 2', () => {
cy.get('.power-grid-table tbody tr').eq(1)
.should('contain.html', 'pgRenderActions({ rowId: 2, parentId: null })')
.should('contain.html', 'wire:click="$dispatch(\'clickToEdit\'')
// Eye icon
.should('contain.html', '<svg class="w-5 text-red-600 !text-green-500" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">\n' +
' <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path>\n' +
' <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"></path>\n' +
'</svg>')

cy.intercept('POST', '/livewire/update', (req) => {
expect(req.body.components[0].calls[0]).to.deep.equal({
path: "",
method: "__dispatch",
params: [
"clickToEdit",
{
action: "view",
name: "Daniel"
}
]
});
}).as('requestIntercept');

cy.get('[data-cy=btn-view-2').click()

cy.wait('@requestIntercept')
.its('response.body')
.should((response) => {
expect(response.components[0].effects.xjs[0])
.to.deep
.equal('console.log("Editing #view - Daniel")');
});
})

it('can render "Edit" button and click for row 2', () => {
cy.get('.power-grid-table tbody tr').eq(1)
.should('contain.html', 'pgRenderActions({ rowId: 2, parentId: null })')
.should('contain.html', 'data-cy="btn-edit-2"')
.should('contain.html', 'class="text-slate-500 items-center flex gap-2 hover:text-slate-700 hover:bg-slate-100 p-1 px-2 rounded"')
.should('contain.html', 'wire:click="$dispatch(\'clickToEdit\'')
// Pencil icon
.should('contain.html', '<svg class="w-5 text-red-600 !text-green-500" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">\n' +
' <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z"></path>\n' +
'</svg>')
.should('contain.text', 'Edit')

cy.intercept('POST', '/livewire/update', (req) => {
expect(req.body.components[0].calls[0]).to.deep.equal({
path: "",
method: "__dispatch",
params: [
"clickToEdit",
{
action: "edit",
name: "Daniel"
}
]
});

}).as('requestIntercept');

cy.get('[data-cy=btn-edit-2]').click()

cy.wait('@requestIntercept')
.its('response.body')
.should((response) => {
expect(response.components[0].effects.xjs[0])
.to.deep
.equal('console.log("Editing #edit - Daniel")');
});
});
})
27 changes: 27 additions & 0 deletions tests/cypress/cypress/e2e/actions/can.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
describe('can render actions buttons with icons', () => {
beforeEach(() => {
cy.visit('/actions-can');
});

it('shoul be able to render "can" properly', () => {
cy.get('.power-grid-table tbody tr').eq(0)
.should('contain.text', '*Cannot*')
.should('not.contain.text', '*Can*')

cy.get('.power-grid-table tbody tr').eq(1)
.should('contain.text', '*Can*')
.should('not.contain.text', '*Cannot*')

cy.get('.power-grid-table tbody tr').eq(2)
.should('contain.text', '*Cannot*')
.should('not.contain.text', '*Can*')

cy.get('.power-grid-table tbody tr').eq(3)
.should('contain.text', '*Cannot*')
.should('not.contain.text', '*Can*')

cy.get('.power-grid-table tbody tr').eq(4)
.should('contain.text', '*Cannot*')
.should('not.contain.text', '*Can*')
});
})
14 changes: 14 additions & 0 deletions tests/cypress/cypress/e2e/actions/disable.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
describe('can render actions buttons with icons', () => {
beforeEach(() => {
cy.visit('/actions-disable');
});

it('shoul be able to render "can" properly', () => {
cy.get('.power-grid-table tbody tr').eq(0)
.should('contain.text', 'Disable')
.should('not.have.attr', 'disabled')

cy.get('.power-grid-table tbody tr').eq(1)
.should('contain.html', '<button disabled="disabled" class="text-slate-500 items-center flex gap-2 hover:text-slate-700 hover:bg-slate-100 p-1 px-2 rounded" data-cy="btn-disable-2">Disable</button>')
});
})