Skip to content

Commit

Permalink
add unit tests and some simple examples
Browse files Browse the repository at this point in the history
  • Loading branch information
gkampitakis committed Jul 21, 2024
1 parent a34dc31 commit c8c7cd8
Show file tree
Hide file tree
Showing 10 changed files with 214 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

<!DOCTYPE html>
<html>
<body>

<h1>My First Heading</h1>

<p>My first paragraph.</p>

</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>Hello World</div>
1 change: 1 addition & 0 deletions examples/__snapshots__/my_standalone_snap_1.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world-0
1 change: 1 addition & 0 deletions examples/__snapshots__/my_standalone_snap_2.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world-1
1 change: 1 addition & 0 deletions examples/__snapshots__/my_standalone_snap_3.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world-2
1 change: 1 addition & 0 deletions examples/__snapshots__/my_standalone_snap_4.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world-3
37 changes: 35 additions & 2 deletions examples/matchStandaloneSnapshot_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,40 @@
package examples

import "testing"
import (
"testing"

"github.com/gkampitakis/go-snaps/snaps"
)

func TestMatchStandaloneSnapshot(t *testing.T) {
t.Log("to be implemented")
t.Run("should create html snapshots", func(t *testing.T) {
snaps := snaps.WithConfig(
snaps.Ext(".html"),
)

snaps.MatchStandaloneSnapshot(t, `
<!DOCTYPE html>
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
`)
snaps.MatchStandaloneSnapshot(t, "<div>Hello World</div>")
})

t.Run("should create standalone snapshots with specified filename", func(t *testing.T) {
snaps := snaps.WithConfig(
snaps.Filename("my_standalone_snap"),
)

snaps.MatchStandaloneSnapshot(t, "hello world-0")
snaps.MatchStandaloneSnapshot(t, "hello world-1")
snaps.MatchStandaloneSnapshot(t, "hello world-2")
snaps.MatchStandaloneSnapshot(t, "hello world-3")
})
}
3 changes: 2 additions & 1 deletion snaps/matchSnapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func setupSnapshot(t *testing.T, file string, ci bool, update ...bool) string {
t.Cleanup(func() {
os.Remove(snapPath)
testsRegistry = newRegistry()
standaloneTestsRegistry = newStandaloneRegistry()
testEvents = newTestEvents()
isCI = ciinfo.IsCI
updateVAR = updateVARPrev
Expand Down Expand Up @@ -78,7 +79,7 @@ func TestMatchSnapshot(t *testing.T) {
test.Equal(t, 1, testsRegistry.cleanup[snapPath]["mock-name"])
})

t.Run("if it's running on ci should skip", func(t *testing.T) {
t.Run("if it's running on ci should skip creating snapshot", func(t *testing.T) {
setupSnapshot(t, fileName, true)

mockT := test.NewMockTestingT(t)
Expand Down
2 changes: 1 addition & 1 deletion snaps/matchStandaloneSnapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func matchStandaloneSnapshot(c *config, t testingT, value any) {
return
}

if !shouldUpdate(nil) {
if !shouldUpdate(c.update) {
handleError(t, diff)
return
}
Expand Down
166 changes: 160 additions & 6 deletions snaps/matchStandaloneSnapshot_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,175 @@
package snaps

import "testing"
import (
"path/filepath"
"testing"

"github.com/gkampitakis/go-snaps/internal/test"
)

const standaloneFilename = "matchStandaloneSnapshot_test_mock-name_1.snap"

func TestMatchStandaloneSnapshot(t *testing.T) {
t.Run("should create snapshot", func(t *testing.T) {
t.Log("to be implemented")
snapPath := setupSnapshot(t, standaloneFilename, false)
mockT := test.NewMockTestingT(t)
mockT.MockLog = func(args ...any) { test.Equal(t, addedMsg, args[0].(string)) }

MatchStandaloneSnapshot(mockT, "hello world")

test.Equal(t, "hello world", test.GetFileContent(t, snapPath))
test.Equal(t, 1, testEvents.items[added])
// clean up function called

registryKey := filepath.Join(
filepath.Dir(snapPath),
"matchStandaloneSnapshot_test_mock-name_%d.snap",
)
test.Equal(t, 0, standaloneTestsRegistry.running[registryKey])
test.Equal(t, 1, standaloneTestsRegistry.cleanup[registryKey])
})

t.Run("should pass tests with no diff", func(t *testing.T) {
snapPath := setupSnapshot(t, standaloneFilename, false, false)

printerExpectedCalls := []func(received any){
func(received any) { test.Equal(t, addedMsg, received.(string)) },
func(received any) { t.Error("should not be called 3rd time") },
}
mockT := test.NewMockTestingT(t)
mockT.MockLog = func(args ...any) {
printerExpectedCalls[0](args[0])

// shift
printerExpectedCalls = printerExpectedCalls[1:]
}

s := WithConfig(Update(true))
// First call for creating the snapshot
s.MatchStandaloneSnapshot(mockT, "hello world")
test.Equal(t, 1, testEvents.items[added])

// Resetting registry to emulate the same MatchStandaloneSnapshot call
standaloneTestsRegistry = newStandaloneRegistry()

// Second call with same params
s.MatchStandaloneSnapshot(mockT, "hello world")

test.Equal(t, "hello world", test.GetFileContent(t, snapPath))
test.Equal(t, 1, testEvents.items[passed])
})

t.Run("if it's running on ci should skip", func(t *testing.T) {
t.Log("to be implemented")
t.Run("if it's running on ci should skip creating snapshot", func(t *testing.T) {
setupSnapshot(t, standaloneFilename, true)

mockT := test.NewMockTestingT(t)
mockT.MockError = func(args ...any) {
test.Equal(t, errSnapNotFound, args[0].(error))
}

MatchStandaloneSnapshot(mockT, "hello world")

test.Equal(t, 1, testEvents.items[erred])
})

t.Run("should return error when diff is found", func(t *testing.T) {
t.Log("to be implemented")
setupSnapshot(t, standaloneFilename, false)

printerExpectedCalls := []func(received any){
func(received any) { test.Equal(t, addedMsg, received.(string)) },
func(received any) { t.Error("should not be called 2nd time") },
}
mockT := test.NewMockTestingT(t)
mockT.MockError = func(args ...any) {
expected := "\n\x1b[38;5;52m\x1b[48;5;225m- Snapshot - 1\x1b[0m\n\x1b[38;5;22m\x1b[48;5;159m" +
"+ Received + 1\x1b[0m\n\n\x1b[48;5;225m\x1b[38;5;52m- \x1b[0m\x1b[48;5;127m\x1b[38;5;255m" +
"hello\x1b[0m\x1b[48;5;225m\x1b[38;5;52m world\x1b[0m\n\x1b[48;5;159m\x1b[38;5;22m" +
"+ \x1b[0m\x1b[48;5;23m\x1b[38;5;255mbye\x1b[0m\x1b[48;5;159m\x1b[38;5;22m world\x1b[0m\n\n\x1b[2m" +
"at " + filepath.FromSlash(
"__snapshots__/matchStandaloneSnapshot_test_mock-name_1.snap:1",
) + "\n\x1b[0m"

test.Equal(t, expected, args[0].(string))
}
mockT.MockLog = func(args ...any) {
printerExpectedCalls[0](args[0])

// shift
printerExpectedCalls = printerExpectedCalls[1:]
}

// First call for creating the snapshot
MatchStandaloneSnapshot(mockT, "hello world")
test.Equal(t, 1, testEvents.items[added])

// Resetting registry to emulate the same MatchStandaloneSnapshot call
standaloneTestsRegistry = newStandaloneRegistry()

// Second call with different data
MatchStandaloneSnapshot(mockT, "bye world")
test.Equal(t, 1, testEvents.items[erred])
})

t.Run("should update snapshot", func(t *testing.T) {
t.Log("to be implemented")
t.Run("when 'updateVAR==true'", func(t *testing.T) {
snapPath := setupSnapshot(t, standaloneFilename, false, true)

printerExpectedCalls := []func(received any){
func(received any) { test.Equal(t, addedMsg, received.(string)) },
func(received any) { test.Equal(t, updatedMsg, received.(string)) },
func(received any) { t.Error("should not be called 3rd time") },
}
mockT := test.NewMockTestingT(t)
mockT.MockLog = func(args ...any) {
printerExpectedCalls[0](args[0])

// shift
printerExpectedCalls = printerExpectedCalls[1:]
}

// First call for creating the snapshot
MatchStandaloneSnapshot(mockT, "hello world")
test.Equal(t, 1, testEvents.items[added])

// Resetting registry to emulate the same MatchStandaloneSnapshot call
standaloneTestsRegistry = newStandaloneRegistry()

// Second call with different params
MatchStandaloneSnapshot(mockT, "bye world")

test.Equal(t, "bye world", test.GetFileContent(t, snapPath))
test.Equal(t, 1, testEvents.items[updated])
})

t.Run("when config update", func(t *testing.T) {
snapPath := setupSnapshot(t, standaloneFilename, false, false)

printerExpectedCalls := []func(received any){
func(received any) { test.Equal(t, addedMsg, received.(string)) },
func(received any) { test.Equal(t, updatedMsg, received.(string)) },
func(received any) { t.Error("should not be called 3rd time") },
}
mockT := test.NewMockTestingT(t)
mockT.MockLog = func(args ...any) {
printerExpectedCalls[0](args[0])

// shift
printerExpectedCalls = printerExpectedCalls[1:]
}

s := WithConfig(Update(true))
// First call for creating the snapshot
s.MatchStandaloneSnapshot(mockT, "hello world")
test.Equal(t, 1, testEvents.items[added])

// Resetting registry to emulate the same MatchStandaloneSnapshot call
standaloneTestsRegistry = newStandaloneRegistry()

// Second call with different params
s.MatchStandaloneSnapshot(mockT, "bye world")

test.Equal(t, "bye world", test.GetFileContent(t, snapPath))
test.Equal(t, 1, testEvents.items[updated])
})
})
}

0 comments on commit c8c7cd8

Please sign in to comment.