gophertype/pkg/mogrify/mogrify_test.go
Noah 8f98e72e47 Lazy load blog post images by default
* Post.HTML() and PreviewHTML() both mogrify the resulting HTML code to
  ensure all <img> tags have loading="lazy" unless a loading attribute
  is already present.
2020-04-09 19:16:41 -07:00

45 rindas
1.0 KiB
Go

package mogrify_test
import (
"testing"
"git.kirsle.net/apps/gophertype/pkg/mogrify"
)
func TestLazyLoadImages(t *testing.T) {
var tests = []struct {
In string
Expect string
}{
{
In: `<img src="logo.jpg">`,
Expect: `<img src="logo.jpg" loading="lazy">`,
},
{
In: `<img src="https://example.com/image.png" loading="eager">`,
Expect: `<img src="https://example.com/image.png" loading="eager">`,
},
{
In: `Hello world`,
Expect: `Hello world`,
},
{
In: `<img src="a"><img src="b"><img src="a">`,
Expect: `<img src="a" loading="lazy"><img src="b" loading="lazy"><img src="a" loading="lazy">`,
},
{
In: `<img src="a"><img src="b" loading="eager"><img src="a">`,
Expect: `<img src="a" loading="lazy"><img src="b" loading="eager"><img src="a" loading="lazy">`,
},
}
for _, test := range tests {
actual := mogrify.LazyLoadImages(test.In)
if actual != test.Expect {
t.Errorf("for input {%s} I expected {%s} but got: {%s}",
test.In, test.Expect, actual,
)
}
}
}