diff --git a/tests/Feature/Recipe/EditTest.php b/tests/Feature/Recipe/EditTest.php new file mode 100644 index 0000000..26a4594 --- /dev/null +++ b/tests/Feature/Recipe/EditTest.php @@ -0,0 +1,160 @@ +create(); + $recipe = Recipe::factory()->create([ + 'steps' => [], + 'ingredients' => [], + ]); + $tag1 = Tag::factory()->create(); + $tag2 = Tag::factory()->create(); + + Livewire::actingAs($user) + ->test('recipe.edit', ['recipe' => $recipe]) + ->set('title', 'New Title') + ->set('description', 'New Description') + ->set('cooking_time', '30') + ->set('type', 'Main Dish') + ->set('video_url', 'https://www.youtube.com/watch?v=dQw4w9WgXcQ') + ->set('image', UploadedFile::fake()->image('new_image.jpg')) + ->set('tags', [$tag1->id, $tag2->id]) + ->call('addIngredient') + ->set('ingredients_name.0', 'New Ingredient') + ->set('ingredients_portion.0', '100') + ->call('addStep') + ->set('steps.0.description', 'New Step Description') + ->set('steps.0.image', UploadedFile::fake()->image('new_step_image.jpg')) + ->call('publish'); + + $this->assertDatabaseHas('recipes', [ + 'title' => 'New Title', + 'description' => 'New Description', + 'cooking_time' => 30, + 'type' => 'Main Dish', + 'video_url' => 'https://www.youtube.com/watch?v=dQw4w9WgXcQ', + 'image' => 'recipes/new_image.jpg', + 'state' => 'active', + ]); + + $this->assertDatabaseHas('recipe_tag', [ + 'recipe_id' => $recipe->id, + 'tag_id' => $tag1->id, + ]); + $this->assertDatabaseHas('recipe_tag', [ + 'recipe_id' => $recipe->id, + 'tag_id' => $tag2->id, + ]); + + $this->assertDatabaseHas('ingredients', [ + 'name' => 'New Ingredient', + 'portion' => '100', + 'unit' => 'gr', + 'recipe_id' => $recipe->id, + ]); + + Storage::disk('public')->assertExists('recipes/new_image.jpg'); + Storage::disk('public')->assertExists('steps/new_step_image.jpg'); + } + + public function can_remove_ingredient() + { + $user = User::factory()->create(); + $recipe = Recipe::factory()->create([ + 'steps' => [], + 'ingredients' => [], + ]); + $ingredient1 = Ingredient::factory()->create(['recipe_id' => $recipe->id]); + $ingredient2 = Ingredient::factory()->create(['recipe_id' => $recipe->id]); + + Livewire::actingAs($user) + ->test('recipe.edit', ['recipe' => $recipe]) + ->call('removeIngredient', 0); + + $this->assertDatabaseMissing('ingredients', [ + 'id' => $ingredient1->id, + ]); + $this->assertDatabaseHas('ingredients', [ + 'id' => $ingredient2->id, + ]); + } + + public function can_remove_step() + { + Storage::fake('public'); + $user = User::factory()->create(); + $recipe = Recipe::factory()->create([ + 'steps' => [], + 'ingredients' => [], + ]); + $step1 = Step::factory()->create(['recipe_id' => $recipe->id]); + $step2 = Step::factory()->create(['recipe_id' => $recipe->id]); + + Livewire::actingAs($user) + ->test('recipe.edit', ['recipe' => $recipe]) + ->call('removeStep', 0); + + $this->assertDatabaseMissing('steps', [ + 'id' => $step1->id, + ]); + $this->assertDatabaseHas('steps', [ + 'id' => $step2->id, + ]); + } + + public function can_add_tag() + { + $user = User::factory()->create(); + $recipe = Recipe::factory()->create([ + 'steps' => [], + 'ingredients' => [], + ]); + $tag = Tag::factory()->create(); + + Livewire::actingAs($user) + ->test('recipe.edit', ['recipe' => $recipe]) + ->emit('tagAdded', $tag->id); + + $this->assertDatabaseHas('recipe_tag', [ + 'recipe_id' => $recipe->id, + 'tag_id' => $tag->id, + ]); + } + + public function can_remove_tag() + { + $user = User::factory()->create(); + $recipe = Recipe::factory()->create([ + 'steps' => [], + 'ingredients' => [], + ]); + $tag = Tag::factory()->create(); + $recipe->tags()->attach($tag->id); + + Livewire::actingAs($user) + ->test('recipe.edit', ['recipe' => $recipe]) + ->emit('tagRemoved', $tag->name); + + $this->assertDatabaseMissing('recipe_tag', [ + 'recipe_id' => $recipe->id, + 'tag_id' => $tag->id, + ]); + } +}