|
| 1 | +package snaps |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/gkampitakis/go-snaps/internal/test" |
| 8 | +) |
| 9 | + |
| 10 | +func TestWithConfig(t *testing.T) { |
| 11 | + t.Run("returns default config when no options provided", func(t *testing.T) { |
| 12 | + c := WithConfig() |
| 13 | + |
| 14 | + test.Equal(t, "__snapshots__", c.snapsDir) |
| 15 | + test.Equal(t, "", c.filename) |
| 16 | + test.Equal(t, "", c.extension) |
| 17 | + test.Nil(t, c.update) |
| 18 | + test.Nil(t, c.json) |
| 19 | + test.Nil(t, c.printer) |
| 20 | + }) |
| 21 | + |
| 22 | + t.Run("Filename", func(t *testing.T) { |
| 23 | + c := WithConfig(Filename("my_test")) |
| 24 | + test.Equal(t, "my_test", c.filename) |
| 25 | + }) |
| 26 | + |
| 27 | + t.Run("Dir", func(t *testing.T) { |
| 28 | + c := WithConfig(Dir("my_dir")) |
| 29 | + test.Equal(t, "my_dir", c.snapsDir) |
| 30 | + }) |
| 31 | + |
| 32 | + t.Run("Ext", func(t *testing.T) { |
| 33 | + c := WithConfig(Ext(".txt")) |
| 34 | + test.Equal(t, ".txt", c.extension) |
| 35 | + }) |
| 36 | + |
| 37 | + t.Run("Update", func(t *testing.T) { |
| 38 | + c := WithConfig(Update(true)) |
| 39 | + test.Equal(t, true, *c.update) |
| 40 | + |
| 41 | + c = WithConfig(Update(false)) |
| 42 | + test.Equal(t, false, *c.update) |
| 43 | + }) |
| 44 | + |
| 45 | + t.Run("JSON", func(t *testing.T) { |
| 46 | + c := WithConfig(JSON(JSONConfig{SortKeys: true, Indent: " ", Width: 80})) |
| 47 | + test.Equal(t, true, c.json.SortKeys) |
| 48 | + test.Equal(t, " ", c.json.Indent) |
| 49 | + test.Equal(t, 80, c.json.Width) |
| 50 | + }) |
| 51 | + |
| 52 | + t.Run("Printer", func(t *testing.T) { |
| 53 | + fn := func(v any) string { return fmt.Sprint(v) } |
| 54 | + c := WithConfig(Printer(fn)) |
| 55 | + test.Equal(t, "hello", c.printer("hello")) |
| 56 | + }) |
| 57 | + |
| 58 | + t.Run("multiple options are all applied", func(t *testing.T) { |
| 59 | + c := WithConfig(Filename("my_test"), Dir("my_dir"), Ext(".txt"), Update(true)) |
| 60 | + test.Equal(t, "my_test", c.filename) |
| 61 | + test.Equal(t, "my_dir", c.snapsDir) |
| 62 | + test.Equal(t, ".txt", c.extension) |
| 63 | + test.Equal(t, true, *c.update) |
| 64 | + }) |
| 65 | + |
| 66 | + t.Run("does not mutate defaultConfig", func(t *testing.T) { |
| 67 | + _ = WithConfig(Filename("my_test"), Dir("my_dir"), Update(true)) |
| 68 | + |
| 69 | + test.Equal(t, "__snapshots__", defaultConfig.snapsDir) |
| 70 | + test.Equal(t, "", defaultConfig.filename) |
| 71 | + test.Nil(t, defaultConfig.update) |
| 72 | + test.Nil(t, defaultConfig.printer) |
| 73 | + }) |
| 74 | +} |
| 75 | + |
| 76 | +func TestTakeSnapshot(t *testing.T) { |
| 77 | + t.Run("falls back to pretty.Sprint when no printer set", func(t *testing.T) { |
| 78 | + result := defaultConfig.takeSnapshot([]any{10, "hello world"}) |
| 79 | + |
| 80 | + test.Equal(t, "int(10)\nhello world", result) |
| 81 | + }) |
| 82 | + |
| 83 | + t.Run("uses custom printer", func(t *testing.T) { |
| 84 | + c := WithConfig(Printer(func(v any) string { |
| 85 | + return fmt.Sprintf("custom:%v", v) |
| 86 | + })) |
| 87 | + |
| 88 | + result := c.takeSnapshot([]any{"world"}) |
| 89 | + |
| 90 | + test.Equal(t, "custom:world", result) |
| 91 | + }) |
| 92 | + |
| 93 | + t.Run("calls printer once per value", func(t *testing.T) { |
| 94 | + calls := 0 |
| 95 | + c := WithConfig(Printer(func(v any) string { |
| 96 | + calls++ |
| 97 | + return fmt.Sprint(v) |
| 98 | + })) |
| 99 | + |
| 100 | + value := c.takeSnapshot([]any{"a", "b", "c"}) |
| 101 | + |
| 102 | + test.Equal(t, 3, calls) |
| 103 | + test.Equal(t, "a\nb\nc", value) |
| 104 | + }) |
| 105 | +} |
| 106 | + |
| 107 | +func TestTakeStandaloneSnapshot(t *testing.T) { |
| 108 | + t.Run("falls back to pretty.Sprint when no printer set", func(t *testing.T) { |
| 109 | + result := defaultConfig.takeStandaloneSnapshot("hello world") |
| 110 | + |
| 111 | + test.Equal(t, "hello world", result) |
| 112 | + }) |
| 113 | + |
| 114 | + t.Run("uses custom printer", func(t *testing.T) { |
| 115 | + c := WithConfig(Printer(func(v any) string { |
| 116 | + return fmt.Sprintf("custom:%v", v) |
| 117 | + })) |
| 118 | + |
| 119 | + result := c.takeStandaloneSnapshot("world") |
| 120 | + |
| 121 | + test.Equal(t, "custom:world", result) |
| 122 | + }) |
| 123 | +} |
| 124 | + |
| 125 | +func TestTakeInlineSnapshot(t *testing.T) { |
| 126 | + t.Run("falls back to pretty.Sprint when no printer set", func(t *testing.T) { |
| 127 | + result := defaultConfig.takeInlineSnapshot("hello world") |
| 128 | + |
| 129 | + test.Equal(t, "hello world", result) |
| 130 | + }) |
| 131 | + |
| 132 | + t.Run("uses custom printer", func(t *testing.T) { |
| 133 | + c := WithConfig(Printer(func(v any) string { |
| 134 | + return fmt.Sprintf("custom:%v", v) |
| 135 | + })) |
| 136 | + |
| 137 | + result := c.takeInlineSnapshot("world") |
| 138 | + |
| 139 | + test.Equal(t, "custom:world", result) |
| 140 | + }) |
| 141 | +} |
0 commit comments