summaryrefslogtreecommitdiff
path: root/tests/Feature/AssetsTest.php
blob: f618d460ebbd9588322f9e90f629116f1a5ef492 (plain)
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
<?php

use Lightscale\LaralightAssets\Assets;
use Lightscale\LaralightAssets\Manifest;
use Lightscale\LaralightAssets\FileString;

describe('instance', function() {
    it('instantiates', function() {
        new Assets();
    })->throwsNoExceptions();

    it('instantiates from container', function() {
        app(Assets::class);
    })->throwsNoExceptions();

    it('is singleton', function() {
        expect(app(Assets::class))->toBe(app(Assets::class));
    });
});

$newManifest = fn() => new Manifest(
    public_path('dist/manifest.json'),
    '/dist',
);

describe('manages manifests', function() use ($newManifest) {
    it('adds manifests', function() use ($newManifest) {
        app(Assets::class)->addManifest('main', $newManifest(), isDefault: true);
    })->throwsNoExceptions();

    it('registers manifests', function() {
        app(Assets::class)->registerManifest(
            'main', public_path('dist/manifest.json'), '/dist', isDefault: true,
        );
    })->throwsNoExceptions();

    it('gets manifests', function() {
        $assets = app(Assets::class);
        $assets->registerManifest(
            'main', public_path('dist/manifest.json'), '/dist', isDefault: true,
        );

        expect($assets->getManifests())
            ->toHaveLength(1)
            ->toContainOnlyInstancesOf(Manifest::class);
    });

    it('gets manifest', function() use ($newManifest){
        $assets = app(Assets::class);
        $manifest = $newManifest();
        $name = 'main';
        $assets->addManifest($name, $manifest, true);

        expect($assets->getManifest($name))->toBe($manifest);
    });

    it('gets default manifest', function() use ($newManifest) {
        $assets = app(Assets::class);
        $manifest = $newManifest();
        $name = 'main';
        $assets->addManifest($name, $manifest, true);

        expect($assets->getDefaultManifest())->toBe($manifest);
    });

    it('throws exception missing manifest', function() {
        $assets = app(Assets::class);
        $assets->getManifest('main');
    })->throws(Exception::class);

    it('has null default manifest')
        ->expect(fn() => app(Assets::class)->getDefaultManifest())
        ->toBeNull();

    it('throws exception setting default to missing manifest', function() {
        $assets = app(Assets::class);
        $assets->setDefaultManifest('testing');
    })->throws(Exception::class);

    test('get manifest from FileString', function() use($newManifest) {
        $assets = app(Assets::class);
        $manifest = $newManifest();
        $assets->addManifest('main', $manifest, true);
        $file = new FileString('main::test.css');
        $result = $assets->getFileManifest($file);
        expect($result)->toBe($manifest);
    });

    test('default manifest from FileString', function() use($newManifest) {
        $assets = app(Assets::class);
        $manifest = $newManifest();
        $assets->addManifest('main', $manifest, true);
        $file = new FileString('test.css');
        $result = $assets->getFileManifest($file);
        expect($result)->toBe($manifest);
    });

});