github.com/jgarto/itcv@v0.0.0-20180826224514-4eea09c1aa0d/cmd/stateGen/_testFiles/template_test.go (about)

     1  package banana_test
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"myitcv.io/react/cmd/stateGen/_testFiles"
     8  )
     9  
    10  func TestBasic(t *testing.T) {
    11  	r := banana.NewRoot()
    12  
    13  	if v := r.Model().Get(); v != nil {
    14  		t.Fatalf("expected nil as initial value; got %v", v)
    15  	}
    16  
    17  	b := bytes.NewBuffer(nil)
    18  	r.Model().Set(b)
    19  
    20  	if v := r.Model().Get(); v != b {
    21  		t.Fatalf("expected value %v; got %v", b, v)
    22  	}
    23  
    24  	cb1Count := 0
    25  	cb2Count := 0
    26  
    27  	cb1 := func() {
    28  		cb1Count++
    29  	}
    30  
    31  	cb2 := func() {
    32  		cb2Count++
    33  	}
    34  
    35  	sub1 := r.TaggingScreen().Subscribe(cb1)
    36  	sub2 := r.TaggingScreen().Name().Subscribe(cb2)
    37  
    38  	if v := r.TaggingScreen().Name().Get(); v != "" {
    39  		t.Fatalf("expected empty string initial value; got %q", v)
    40  	}
    41  
    42  	setCount1 := 1
    43  	setCount2 := 1
    44  
    45  	r.TaggingScreen().Name().Set("hello")
    46  
    47  	if cb1Count != setCount1 {
    48  		t.Fatalf("expected cb1Count to be %v; got %v", setCount1, cb1Count)
    49  	}
    50  	if cb2Count != setCount2 {
    51  		t.Fatalf("expected cb2Count to be %v; got %v", setCount2, cb2Count)
    52  	}
    53  
    54  	// leave the value the same... shouldn't get callback
    55  	r.TaggingScreen().Name().Set("hello")
    56  
    57  	if cb1Count != setCount1 {
    58  		t.Fatalf("expected cb1Count to be %v; got %v", setCount1, cb1Count)
    59  	}
    60  	if cb2Count != setCount2 {
    61  		t.Fatalf("expected cb2Count to be %v; got %v", setCount2, cb2Count)
    62  	}
    63  
    64  	sub1.Clear()
    65  
    66  	setCount2++
    67  	r.TaggingScreen().Name().Set("again")
    68  
    69  	if cb1Count != setCount1 {
    70  		t.Fatalf("expected cb1Count to be %v; got %v", setCount1, cb1Count)
    71  	}
    72  	if cb2Count != setCount2 {
    73  		t.Fatalf("expected cb2Count to be %v; got %v", setCount2, cb2Count)
    74  	}
    75  
    76  	sub2.Clear()
    77  
    78  	r.TaggingScreen().Name().Set("hello")
    79  
    80  	if cb1Count != setCount1 {
    81  		t.Fatalf("expected cb1Count to be %v; got %v", setCount1, cb1Count)
    82  	}
    83  	if cb2Count != setCount2 {
    84  		t.Fatalf("expected cb2Count to be %v; got %v", setCount2, cb2Count)
    85  	}
    86  }