-
Notifications
You must be signed in to change notification settings - Fork 44
/
DependentActionFieldTest.php
217 lines (200 loc) · 10.3 KB
/
DependentActionFieldTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
namespace Laravel\Nova\Tests\Browser;
use App\Models\User;
use Database\Factories\CaptainFactory;
use Database\Factories\PostFactory;
use Laravel\Dusk\Browser;
use Laravel\Nova\Notifications\NovaNotification;
use Laravel\Nova\Testing\Browser\Components\ActionDropdownComponent;
use Laravel\Nova\Testing\Browser\Components\Controls\RelationSelectControlComponent;
use Laravel\Nova\Testing\Browser\Components\DetailComponent;
use Laravel\Nova\Testing\Browser\Components\IndexComponent;
use Laravel\Nova\Testing\Browser\Components\Modals\ConfirmActionModalComponent;
use Laravel\Nova\Testing\Browser\Pages\Detail;
use Laravel\Nova\Testing\Browser\Pages\Index;
use Laravel\Nova\Testing\Browser\Pages\UserIndex;
use Laravel\Nova\Tests\DuskTestCase;
/**
* @covers \Laravel\Nova\Fields\Field::dependsOn()
* @covers \Laravel\Nova\Http\Controllers\ActionController::sync()
*/
class DependentActionFieldTest extends DuskTestCase
{
public function test_it_can_sync_dependent_fields()
{
$this->browse(function (Browser $browser) {
$browser->loginAs(1)
->visit(new UserIndex)
->within(new IndexComponent('users'), function (Browser $browser) {
$browser->clickCheckboxForId(1)
->runAction('send-notification', function ($browser) {
$browser->assertSelected('@type', '')
->assertDisabled('@message')
->assertDisabled('@icon')
->select('@type', 'success')
->waitFor('@action_url')
->assertVisible('textarea[dusk="message"]')
->assertMissing('input[type="text"][dusk="message"]')
->type('@action_url', 'https://nova.laravel.com/released/4.0.0')
->typeSlowly('@message', 'Laravel Nova has been released')
->type('@action_text', 'View Release')
->select('@icon', 'download');
});
})->waitForText('The action was executed successfully.');
$this->assertDatabaseHas('nova_notifications', [
'type' => NovaNotification::class,
'notifiable_type' => User::class,
'notifiable_id' => 1,
'data' => json_encode([
'component' => 'message-notification',
'icon' => 'download',
'message' => 'Laravel Nova has been released',
'actionText' => 'View Release',
'actionUrl' => [
'url' => 'https://nova.laravel.com/released/4.0.0',
'remote' => true,
],
'openInNewTab' => false,
'type' => 'success',
'iconClass' => 'text-green-500',
]),
'read_at' => null,
]);
$browser->within(new IndexComponent('users'), function (Browser $browser) {
$browser->clickCheckboxForId(2)
->runAction('send-notification', function ($browser) {
$browser->assertSelected('@type', '')
->assertDisabled('@message')
->assertDisabled('@icon')
->select('@type', 'warning')
->pause(1000)
->assertMissing('textarea[dusk="message"]')
->assertVisible('input[type="text"][dusk="message"]')
->assertMissing('@action_url')
->assertMissing('@action_text')
->typeSlowly('@message', 'Please change your password')
->select('@icon', 'exclamation-circle');
});
})->waitForText('The action was executed successfully.');
$this->assertDatabaseHas('nova_notifications', [
'type' => NovaNotification::class,
'notifiable_type' => User::class,
'notifiable_id' => 2,
'data' => json_encode([
'component' => 'message-notification',
'icon' => 'exclamation-circle',
'message' => 'Please change your password',
'actionText' => 'View',
'actionUrl' => null,
'openInNewTab' => false,
'type' => 'warning',
'iconClass' => 'text-yellow-500',
]),
'read_at' => null,
]);
$browser->blank();
});
}
public function test_it_can_sync_dependent_belongs_to_field()
{
$this->browse(function (Browser $browser) {
$post = PostFactory::new()->create();
$browser->loginAs(1)
->visit(new Index('posts'))
->within(new IndexComponent('posts'), function (Browser $browser) use ($post) {
$browser->waitForTable()
->clickCheckboxForId($post->id)
->runAction('add-comment', function ($browser) {
$browser
->type('@body', 'Hello world')
->assertAttribute('@anonymous-default-boolean-field', 'data-state', 'checked');
});
})
->waitForText('The action was executed successfully.');
$this->assertDatabaseHas('comments', [
'commentable_type' => $post->getMorphClass(),
'commentable_id' => $post->id,
'user_id' => null,
'body' => 'Hello world',
]);
$browser->within(new IndexComponent('posts'), function (Browser $browser) use ($post) {
$browser->clickCheckboxForId($post->id)
->runAction('add-comment', function ($browser) {
$browser->type('@body', 'Hello world with userId')
->assertMissing('@user')
->assertAttribute('@anonymous-default-boolean-field', 'data-state', 'checked')
->check('@anonymous-default-boolean-field')
->pause(1000)
->whenAvailable(new RelationSelectControlComponent('users'), function ($browser) {
$browser->select('', 4);
});
});
})->waitForText('The action was executed successfully.');
$this->assertDatabaseHas('comments', [
'commentable_type' => $post->getMorphClass(),
'commentable_id' => $post->id,
'user_id' => 4,
'body' => 'Hello world with userId',
]);
$browser->blank();
});
}
public function test_it_can_sync_dependent_select_field()
{
$this->browse(function (Browser $browser) {
$browser->loginAs(1)
->visit(new UserIndex)
->within(new IndexComponent('users'), function (Browser $browser) {
$browser->waitForTable()
->runInlineAction(4, 'create-user-profile', function ($browser) {
$browser->assertSelected('@timezone', 'UTC')
->type('@github', 'crynobone')
->assertSelected('@timezone', 'Asia/Kuala_Lumpur');
});
})->waitForText('User Profile created');
$browser->blank();
});
}
public function test_it_provides_selected_resources_information()
{
$this->browse(function (Browser $browser) {
$captains = CaptainFactory::new()->times(5)->create();
$browser->loginAs(1)
->visit(new Index('captains'))
->within(new IndexComponent('captains'), function ($browser) use ($captains) {
$browser->waitForTable()
->selectAllOnCurrentPage()
->selectAction('track-selected-action', function ($browser) use ($captains) {
$browser->assertValue('@selected_resources', 'false - '.$captains->pluck('id')->reverse()->join(','))
->click("[id='toggle-default-boolean-field']")
->pause(1000)
->assertValue('@selected_resources', 'true - '.$captains->pluck('id')->reverse()->join(','))
->cancel();
})
->unselectAllOnCurrentPage()
->selectAllMatching()
->selectAction('track-selected-action', function ($browser) {
$browser->assertValue('@selected_resources', 'false - all')
->click('[id="toggle-default-boolean-field"]')
->pause(1000)
->assertValue('@selected_resources', 'true - all')
->cancel();
});
})
->visit(new Detail('captains', $captains[0]->getKey()))
->within(new DetailComponent('captains', $captains[0]->getKey()), function ($browser) use ($captains) {
$browser->openControlSelector()
->elsewhereWhenAvailable(new ActionDropdownComponent(), function ($browser) use ($captains) {
$browser->click("button[data-action-id='track-selected-action']")
->elsewhereWhenAvailable(new ConfirmActionModalComponent(), function ($browser) use ($captains) {
$browser->assertValue('@selected_resources', 'false - '.$captains[0]->getKey())
->click('[id="toggle-default-boolean-field"]')
->pause(1000)
->assertValue('@selected_resources', 'true - '.$captains[0]->getKey())
->cancel();
});
});
});
});
}
}