github.com/jamiefdhurst/journal@v0.9.2/internal/app/model/giphy_test.go (about)

     1  package model
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/jamiefdhurst/journal/internal/app"
     8  
     9  	"github.com/jamiefdhurst/journal/test/mocks/adapter"
    10  )
    11  
    12  func TestGiphyAdapter_NoAdapterInContainer(t *testing.T) {
    13  
    14  	// Test no adapter
    15  	container := &app.Container{}
    16  	gs := GiphyAdapter(container)
    17  	if reflect.TypeOf(gs).String() != "*model.GiphysDisabled" {
    18  		t.Errorf("Expected GiphysDisabled type, got '%s'", reflect.TypeOf(gs).String())
    19  	}
    20  
    21  	// Test adapter
    22  	client := &adapter.MockGiphyAdapter{}
    23  	container = &app.Container{Giphy: client}
    24  	gs = GiphyAdapter(container)
    25  	if reflect.TypeOf(gs).String() != "*model.Giphys" {
    26  		t.Errorf("Expected Giphys type, got '%s'", reflect.TypeOf(gs).String())
    27  	}
    28  }
    29  
    30  func TestGiphysDisabled_ExtractContentsAndSearchAPI(t *testing.T) {
    31  	testString := "Hello\n:gif:id:1234567\n:gif:testsearch\n"
    32  	gs := GiphysDisabled{}
    33  	newString := gs.ExtractContentsAndSearchAPI(testString)
    34  	if newString != "Hello\n:gif:id:1234567\n:gif:testsearch\n" {
    35  		t.Errorf("Expected no string changes to have happened")
    36  	}
    37  }
    38  
    39  func TestGiphys_ConvertIDsToIframes(t *testing.T) {
    40  	testString := "Hello\n:gif:id:1234567\n:gif:testsearch"
    41  	gs := Giphys{}
    42  	newString := gs.ConvertIDsToIframes(testString)
    43  	if newString != "Hello\n<iframe src=\"https://giphy.com/embed/1234567\"></iframe>\n:gif:testsearch" {
    44  		t.Errorf("Expected iframe substitution did not occur")
    45  	}
    46  }
    47  
    48  func TestGiphys_ExtractContentsAndSearchAPI(t *testing.T) {
    49  
    50  	// Test without error
    51  	testString := "Hello\n:gif:id:1234567\n:gif:testsearch\n"
    52  	client := &adapter.MockGiphyAdapter{}
    53  	container := &app.Container{Giphy: client}
    54  	gs := Giphys{Container: container}
    55  	newString := gs.ExtractContentsAndSearchAPI(testString)
    56  	if newString != "Hello\n:gif:id:1234567\n:gif:id:9991234\n" {
    57  		t.Errorf("Expected search to have been converted")
    58  	}
    59  
    60  	// Test with error
    61  	client.ErrorMode = true
    62  	gs = Giphys{Container: container}
    63  	newString = gs.ExtractContentsAndSearchAPI(testString)
    64  	if newString != "Hello\n:gif:id:1234567\n\n" {
    65  		t.Errorf("Expected search to have been converted and error to have been handled")
    66  	}
    67  }