|
| 1 | +<?php |
| 2 | + |
| 3 | +uses(\Thoughtco\StatamicABTester\Tests\TestCase::class); |
| 4 | + |
| 5 | +use Thoughtco\StatamicABTester\Facades\Experiment; |
| 6 | +use Thoughtco\StatamicABTester\Models\AbTestResult; |
| 7 | +use Thoughtco\StatamicABTester\Widgets\ABTesterWidget; |
| 8 | + |
| 9 | +function makeWidget(): ABTesterWidget |
| 10 | +{ |
| 11 | + $widget = new ABTesterWidget; |
| 12 | + $widget->setConfig([]); |
| 13 | + |
| 14 | + return $widget; |
| 15 | +} |
| 16 | + |
| 17 | +describe('ABTesterWidget', function () { |
| 18 | + it('is registered with the correct handle', function () { |
| 19 | + expect(ABTesterWidget::handle())->toBe('ab_tester'); |
| 20 | + }); |
| 21 | + |
| 22 | + it('renders HTML', function () { |
| 23 | + $html = makeWidget()->html(); |
| 24 | + |
| 25 | + expect($html)->toBeString()->not->toBeEmpty(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('shows a count of zero when there are no active experiments', function () { |
| 29 | + $html = makeWidget()->html(); |
| 30 | + |
| 31 | + expect($html)->toContain('0'); |
| 32 | + expect($html)->toContain('No experiments are currently running.'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('counts active experiments', function () { |
| 36 | + tap(Experiment::make('active-1')->title('Active One')->published(true))->save(); |
| 37 | + tap(Experiment::make('active-2')->title('Active Two')->published(true))->save(); |
| 38 | + |
| 39 | + $html = makeWidget()->html(); |
| 40 | + |
| 41 | + expect($html)->toContain('2'); |
| 42 | + expect($html)->toContain('Active One'); |
| 43 | + expect($html)->toContain('Active Two'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('excludes completed experiments', function () { |
| 47 | + tap(Experiment::make('active')->title('Active')->published(true))->save(); |
| 48 | + |
| 49 | + tap(Experiment::make('done')->title('Done')->published(true)) |
| 50 | + ->completedAt(now()) |
| 51 | + ->save(); |
| 52 | + |
| 53 | + $html = makeWidget()->html(); |
| 54 | + |
| 55 | + expect($html)->toContain('1'); |
| 56 | + expect($html)->toContain('Active'); |
| 57 | + expect($html)->not->toContain('Done'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('excludes unpublished experiments', function () { |
| 61 | + tap(Experiment::make('published')->title('Published')->published(true))->save(); |
| 62 | + tap(Experiment::make('draft')->title('Draft')->published(false))->save(); |
| 63 | + |
| 64 | + $html = makeWidget()->html(); |
| 65 | + |
| 66 | + expect($html)->toContain('1'); |
| 67 | + expect($html)->toContain('Published'); |
| 68 | + expect($html)->not->toContain('Draft'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('excludes experiments that have not started yet', function () { |
| 72 | + tap(Experiment::make('future')->title('Future')->published(true)) |
| 73 | + ->startAt(now()->addDay()) |
| 74 | + ->save(); |
| 75 | + |
| 76 | + $html = makeWidget()->html(); |
| 77 | + |
| 78 | + expect($html)->toContain('0'); |
| 79 | + expect($html)->not->toContain('Future'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('excludes experiments past their end date', function () { |
| 83 | + tap(Experiment::make('expired')->title('Expired')->published(true)) |
| 84 | + ->endAt(now()->subDay()) |
| 85 | + ->save(); |
| 86 | + |
| 87 | + $html = makeWidget()->html(); |
| 88 | + |
| 89 | + expect($html)->toContain('0'); |
| 90 | + expect($html)->not->toContain('Expired'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('shows total hits for each experiment', function () { |
| 94 | + $experiment = tap(Experiment::make('hit-test')->title('Hit Test')->published(true))->save(); |
| 95 | + |
| 96 | + AbTestResult::create(['experiment_id' => $experiment->id(), 'variation' => '1', 'type' => 'hit', 'data' => []]); |
| 97 | + AbTestResult::create(['experiment_id' => $experiment->id(), 'variation' => '1', 'type' => 'hit', 'data' => []]); |
| 98 | + AbTestResult::create(['experiment_id' => $experiment->id(), 'variation' => '2', 'type' => 'hit', 'data' => []]); |
| 99 | + |
| 100 | + $html = makeWidget()->html(); |
| 101 | + |
| 102 | + expect($html)->toContain('3'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('shows the leading variant label', function () { |
| 106 | + $experiment = tap(Experiment::make('leader-test')->title('Leader Test')->published(true))->save(); |
| 107 | + |
| 108 | + // variant 1: 1 hit, 1 success = 100% rate |
| 109 | + AbTestResult::create(['experiment_id' => $experiment->id(), 'variation' => 'control', 'type' => 'hit', 'data' => []]); |
| 110 | + AbTestResult::create(['experiment_id' => $experiment->id(), 'variation' => 'control', 'type' => 'success', 'data' => []]); |
| 111 | + // variant 2: 10 hits, 0 successes = 0% rate |
| 112 | + for ($i = 0; $i < 10; $i++) { |
| 113 | + AbTestResult::create(['experiment_id' => $experiment->id(), 'variation' => 'variant_a', 'type' => 'hit', 'data' => []]); |
| 114 | + } |
| 115 | + |
| 116 | + $html = makeWidget()->html(); |
| 117 | + |
| 118 | + // control has a higher conversion rate so it should appear as the leader |
| 119 | + expect($html)->toContain('control'); |
| 120 | + }); |
| 121 | + |
| 122 | + it('links each experiment to its show page', function () { |
| 123 | + $experiment = tap(Experiment::make('link-test')->title('Link Test')->published(true))->save(); |
| 124 | + |
| 125 | + $html = makeWidget()->html(); |
| 126 | + |
| 127 | + expect($html)->toContain(cp_route('ab.experiments.show', $experiment->id())); |
| 128 | + }); |
| 129 | + |
| 130 | + it('includes a link to the experiments index', function () { |
| 131 | + $html = makeWidget()->html(); |
| 132 | + |
| 133 | + expect($html)->toContain(cp_route('ab.experiments.index')); |
| 134 | + }); |
| 135 | +}); |
0 commit comments