AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

DesignSectionBoxesTest.php 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace Tests\Feature\Stores;
  3. use App\Models\Store;
  4. use App\Models\User;
  5. use App\StorePlan\SamplePlan;
  6. use App\StorePlan\SectionEditor;
  7. use Illuminate\Foundation\Testing\RefreshDatabase;
  8. use Inertia\Testing\AssertableInertia as Assert;
  9. use Tests\TestCase;
  10. class DesignSectionBoxesTest extends TestCase
  11. {
  12. use RefreshDatabase;
  13. private User $user;
  14. private Store $store;
  15. protected function setUp(): void
  16. {
  17. parent::setUp();
  18. $this->user = User::factory()->create();
  19. $this->store = Store::factory()->for($this->user)->create();
  20. $plan = SamplePlan::for($this->store);
  21. // Start without the sample's bento, so adding one is clear.
  22. $this->store->plan = SectionEditor::remove($plan, 'highlights');
  23. $this->store->save();
  24. $this->actingAs($this->user);
  25. }
  26. /**
  27. * @return list<string>
  28. */
  29. private function homeIds(): array
  30. {
  31. return array_column($this->store->fresh()->plan['pages'][0]['sections'], 'id');
  32. }
  33. /**
  34. * @return array<string, mixed>
  35. */
  36. private function section(string $id): array
  37. {
  38. return collect($this->store->fresh()->plan['pages'][0]['sections'])->firstWhere('id', $id);
  39. }
  40. public function test_a_video_can_be_added_after_a_section_changed_and_removed()
  41. {
  42. $this->post(route('stores.videos.store', $this->store->slug), [
  43. 'video_url' => 'https://youtu.be/otej7WLdPh0?si=eYqNO3QE14avO4VL',
  44. 'heading' => 'How we make it',
  45. 'text' => '',
  46. 'variant' => 'wide',
  47. 'after' => 'about',
  48. ])->assertSessionHasNoErrors();
  49. $ids = $this->homeIds();
  50. $this->assertSame('video', $ids[array_search('about', $ids, true) + 1]);
  51. $this->assertSame('https://youtu.be/otej7WLdPh0?si=eYqNO3QE14avO4VL', $this->section('video')['content']['videoUrl']);
  52. $this->get(route('stores.show', $this->store->slug))
  53. ->assertInertia(fn (Assert $page) => $page
  54. ->where('videoSections.0.section_id', 'video')
  55. ->where('videoSections.0.heading', 'How we make it')
  56. ->where('homeSections.0.id', 'hero'));
  57. $this->put(route('stores.videos.update', [$this->store->slug, 'video']), [
  58. 'video_url' => 'https://vimeo.com/76979871', 'heading' => 'Our kitchen', 'text' => 'A day with Nenek.', 'variant' => 'split',
  59. ])->assertSessionHasNoErrors();
  60. $video = $this->section('video');
  61. $this->assertSame('split', $video['variant']);
  62. $this->assertSame('A day with Nenek.', $video['content']['text']);
  63. // A second video gets its own id.
  64. $this->post(route('stores.videos.store', $this->store->slug), [
  65. 'video_url' => 'https://www.youtube.com/watch?v=dQw4w9WgXcQ', 'heading' => 'Second', 'variant' => 'wide', 'after' => 'hero',
  66. ])->assertSessionHasNoErrors();
  67. $this->assertContains('video-2', $this->homeIds());
  68. $this->delete(route('stores.videos.destroy', [$this->store->slug, 'video']))->assertSessionHasNoErrors();
  69. $this->assertNotContains('video', $this->homeIds());
  70. }
  71. public function test_video_links_and_sections_are_checked()
  72. {
  73. $url = route('stores.videos.store', $this->store->slug);
  74. $this->post($url, ['video_url' => 'https://evil.example/v.mp4', 'heading' => 'X', 'variant' => 'wide'])->assertSessionHasErrors('video_url');
  75. $this->post($url, ['video_url' => 'https://youtu.be/otej7WLdPh0', 'heading' => '', 'variant' => 'wide'])->assertSessionHasErrors('heading');
  76. $this->post($url, ['video_url' => 'https://youtu.be/otej7WLdPh0', 'heading' => 'X', 'variant' => 'wide', 'after' => 'nowhere'])->assertSessionHasErrors('after');
  77. // Only video sections can be edited here.
  78. $this->put(route('stores.videos.update', [$this->store->slug, 'about']), [
  79. 'video_url' => 'https://youtu.be/otej7WLdPh0', 'heading' => 'X', 'variant' => 'wide',
  80. ])->assertNotFound();
  81. $this->actingAs(User::factory()->create())
  82. ->post($url, ['video_url' => 'https://youtu.be/otej7WLdPh0', 'heading' => 'X', 'variant' => 'wide'])
  83. ->assertNotFound();
  84. }
  85. public function test_bento_tiles_start_filled_and_can_be_edited()
  86. {
  87. $product = $this->store->products()->create(['name' => 'Sambal', 'price' => 1000, 'stock' => 3, 'status' => 'active']);
  88. $product->images()->create(['store_id' => $this->store->id, 'path' => 'products/1/s.jpg', 'position' => 1]);
  89. $collection = $this->store->collections()->create(['name' => 'Gift sets']);
  90. $this->post(route('stores.bento.store', $this->store->slug), ['after' => 'trust'])->assertSessionHasNoErrors();
  91. $ids = $this->homeIds();
  92. $this->assertSame('highlights', $ids[array_search('trust', $ids, true) + 1]);
  93. $tiles = $this->section('highlights')['content']['tiles'];
  94. $this->assertSame(['/shop', '/collections/'.$collection->slug, '#about'], array_column($tiles, 'href'));
  95. $this->assertSame('product:'.$product->slug, $tiles[0]['image']);
  96. $this->get(route('stores.show', $this->store->slug))
  97. ->assertInertia(fn (Assert $page) => $page
  98. ->where('bentoSections.0.tiles.1.title', 'Gift sets')
  99. ->where('linkSuggestions.1.href', '/collections/gift-sets'));
  100. $this->put(route('stores.bento.update', [$this->store->slug, 'highlights']), [
  101. 'heading' => 'Start here',
  102. 'variant' => 'even',
  103. 'tiles' => [
  104. ['title' => 'Everything', 'text' => '', 'href' => '/shop', 'image' => null, 'tone' => 'primary'],
  105. ['title' => 'Gifts', 'text' => 'Ready to give', 'href' => '/collections/gift-sets', 'image' => 'product:'.$product->slug, 'tone' => 'accent'],
  106. ['title' => 'Story', 'href' => '#about', 'tone' => 'dark'],
  107. ['title' => 'FAQ', 'href' => '#faq', 'tone' => 'surface'],
  108. ],
  109. ])->assertSessionHasNoErrors();
  110. $bento = $this->section('highlights');
  111. $this->assertSame('even', $bento['variant']);
  112. $this->assertSame('Start here', $bento['content']['heading']);
  113. $this->assertCount(4, $bento['content']['tiles']);
  114. $this->assertNull($bento['content']['tiles'][0]['text']);
  115. $this->delete(route('stores.bento.destroy', [$this->store->slug, 'highlights']))->assertSessionHasNoErrors();
  116. $this->assertNotContains('highlights', $this->homeIds());
  117. }
  118. public function test_bento_tiles_are_checked()
  119. {
  120. $this->post(route('stores.bento.store', $this->store->slug), ['after' => 'hero']);
  121. $url = route('stores.bento.update', [$this->store->slug, 'highlights']);
  122. $tile = ['title' => 'Shop', 'href' => '/shop', 'tone' => 'primary'];
  123. $before = $this->section('highlights');
  124. $this->put($url, ['variant' => 'mosaic', 'tiles' => [$tile, $tile]])->assertSessionHasErrors('tiles');
  125. $this->put($url, ['variant' => 'mosaic', 'tiles' => [$tile, $tile, [...$tile, 'href' => 'javascript:alert(1)']]])->assertSessionHasErrors('tiles.2.href');
  126. $this->put($url, ['variant' => 'mosaic', 'tiles' => [$tile, $tile, [...$tile, 'image' => 'product:not-ours']]])->assertSessionHasErrors('section');
  127. // A link to a page the store does not have is caught by the plan checker.
  128. $this->put($url, ['variant' => 'mosaic', 'tiles' => [$tile, $tile, [...$tile, 'href' => '/no-such-page']]])->assertSessionHasErrors('section');
  129. $this->assertSame($before, $this->section('highlights'));
  130. }
  131. }