flamingo.me/flamingo-commerce/v3@v3.11.0/cart/interfaces/templatefunctions/getItemAdjustment_test.go (about)

     1  package templatefunctions
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"flamingo.me/flamingo/v3/framework/web"
     8  	"github.com/google/go-cmp/cmp"
     9  	"github.com/stretchr/testify/assert"
    10  
    11  	"flamingo.me/flamingo-commerce/v3/cart/application"
    12  	"flamingo.me/flamingo-commerce/v3/cart/domain/cart"
    13  	"flamingo.me/flamingo-commerce/v3/cart/domain/validation"
    14  )
    15  
    16  func TestGetQuantityAdjustmentDeletedItemsMessages_Func(t *testing.T) {
    17  	type args struct {
    18  		contextProvider func() context.Context
    19  	}
    20  	tests := []struct {
    21  		name string
    22  		args args
    23  		want []QuantityAdjustment
    24  	}{
    25  		{
    26  			name: "no adjustments returns empty slice",
    27  			args: args{
    28  				contextProvider: func() context.Context {
    29  					session := web.EmptySession()
    30  
    31  					return web.ContextWithSession(context.Background(), session)
    32  				},
    33  			},
    34  			want: []QuantityAdjustment{},
    35  		},
    36  		{
    37  			name: "adjustment that was not deleted returns empty slice",
    38  			args: args{
    39  				contextProvider: func() context.Context {
    40  					session := web.EmptySession()
    41  					session.Store("cart.view.quantity.adjustments", application.QtyAdjustmentResults{
    42  						{
    43  							WasDeleted: false,
    44  						},
    45  					})
    46  
    47  					return web.ContextWithSession(context.Background(), session)
    48  				},
    49  			},
    50  			want: []QuantityAdjustment{},
    51  		},
    52  		{
    53  			name: "adjustment that was deleted returns respective adjustment",
    54  			args: args{
    55  				contextProvider: func() context.Context {
    56  					session := web.EmptySession()
    57  					session.Store("cart.view.quantity.adjustments", application.QtyAdjustmentResults{
    58  						{
    59  							OriginalItem: cart.Item{
    60  								ID: "itemID",
    61  							},
    62  							DeliveryCode: "deliveryCode",
    63  							WasDeleted:   true,
    64  							RestrictionResult: &validation.RestrictionResult{
    65  								RemainingDifference: -2,
    66  								RestrictorName:      "restrictor",
    67  							},
    68  							NewQty: 0,
    69  						},
    70  					})
    71  
    72  					return web.ContextWithSession(context.Background(), session)
    73  				},
    74  			},
    75  			want: []QuantityAdjustment{
    76  				{
    77  					Item: cart.Item{
    78  						ID: "itemID",
    79  					},
    80  					DeliveryCode: "deliveryCode",
    81  					PrevQty:      2,
    82  					CurrQty:      0,
    83  					Reason:       "restrictor",
    84  				},
    85  			},
    86  		},
    87  		{
    88  			name: "returns only deleted adjustment",
    89  			args: args{
    90  				contextProvider: func() context.Context {
    91  					session := web.EmptySession()
    92  					session.Store("cart.view.quantity.adjustments", application.QtyAdjustmentResults{
    93  						{
    94  							OriginalItem: cart.Item{
    95  								ID: "itemID-A",
    96  							},
    97  							DeliveryCode: "deliveryCode-A",
    98  							WasDeleted:   false,
    99  							RestrictionResult: &validation.RestrictionResult{
   100  								RemainingDifference: -2,
   101  								RestrictorName:      "restrictor-A",
   102  							},
   103  							NewQty: 1,
   104  						},
   105  						{
   106  							OriginalItem: cart.Item{
   107  								ID: "itemID-B",
   108  							},
   109  							DeliveryCode: "deliveryCode-B",
   110  							WasDeleted:   true,
   111  							RestrictionResult: &validation.RestrictionResult{
   112  								RemainingDifference: -4,
   113  								RestrictorName:      "restrictor-B",
   114  							},
   115  							NewQty: 0,
   116  						},
   117  					})
   118  
   119  					return web.ContextWithSession(context.Background(), session)
   120  				},
   121  			},
   122  			want: []QuantityAdjustment{
   123  				{
   124  					Item: cart.Item{
   125  						ID: "itemID-B",
   126  					},
   127  					DeliveryCode: "deliveryCode-B",
   128  					PrevQty:      4,
   129  					CurrQty:      0,
   130  					Reason:       "restrictor-B",
   131  				},
   132  			},
   133  		},
   134  	}
   135  	for _, tt := range tests {
   136  		t.Run(tt.name, func(t *testing.T) {
   137  			gdm := &GetQuantityAdjustmentDeletedItemsMessages{}
   138  			getQuantityAdjustmentDeletedItemsMessages := gdm.Func(tt.args.contextProvider()).(func() []QuantityAdjustment)
   139  			if got := getQuantityAdjustmentDeletedItemsMessages(); cmp.Diff(got, tt.want) != "" {
   140  				t.Errorf("Func() = %v, want %v", got, tt.want)
   141  			}
   142  		})
   143  	}
   144  }
   145  
   146  func TestGetQuantityAdjustmentUpdatedItemsMessage_Func(t *testing.T) {
   147  	type args struct {
   148  		contextProvider func() context.Context
   149  		item            cart.Item
   150  		deliveryCode    string
   151  	}
   152  	tests := []struct {
   153  		name string
   154  		args args
   155  		want QuantityAdjustment
   156  	}{
   157  		{
   158  			name: "no adjustments returns adjustment with prev qty = curr qty",
   159  			args: args{
   160  				contextProvider: func() context.Context {
   161  					session := web.EmptySession()
   162  
   163  					return web.ContextWithSession(context.Background(), session)
   164  				},
   165  				item: cart.Item{
   166  					ID:  "itemID",
   167  					Qty: 3,
   168  				},
   169  				deliveryCode: "deliveryCode",
   170  			},
   171  			want: QuantityAdjustment{
   172  				Item: cart.Item{
   173  					ID:  "itemID",
   174  					Qty: 3,
   175  				},
   176  				DeliveryCode: "deliveryCode",
   177  				PrevQty:      3,
   178  				CurrQty:      3,
   179  			},
   180  		},
   181  		{
   182  			name: "not found adjustment returns adjustment with prev qty = curr qty",
   183  			args: args{
   184  				contextProvider: func() context.Context {
   185  					session := web.EmptySession()
   186  					session.Store("cart.view.quantity.adjustments", application.QtyAdjustmentResults{})
   187  
   188  					return web.ContextWithSession(context.Background(), session)
   189  				},
   190  				item: cart.Item{
   191  					ID:  "itemID",
   192  					Qty: 3,
   193  				},
   194  				deliveryCode: "deliveryCode",
   195  			},
   196  			want: QuantityAdjustment{
   197  				Item: cart.Item{
   198  					ID:  "itemID",
   199  					Qty: 3,
   200  				},
   201  				DeliveryCode: "deliveryCode",
   202  				PrevQty:      3,
   203  				CurrQty:      3,
   204  			},
   205  		},
   206  		{
   207  			name: "found adjustment returns respective adjustment",
   208  			args: args{
   209  				contextProvider: func() context.Context {
   210  					session := web.EmptySession()
   211  					session.Store("cart.view.quantity.adjustments", application.QtyAdjustmentResults{
   212  						{
   213  							OriginalItem: cart.Item{
   214  								ID:  "itemID",
   215  								Qty: 1,
   216  							},
   217  							DeliveryCode: "deliveryCode",
   218  							RestrictionResult: &validation.RestrictionResult{
   219  								IsRestricted:        true,
   220  								MaxAllowed:          1,
   221  								RemainingDifference: -2,
   222  								RestrictorName:      "restrictor",
   223  							},
   224  							NewQty: 1,
   225  						},
   226  					})
   227  					return web.ContextWithSession(context.Background(), session)
   228  				},
   229  				item: cart.Item{
   230  					ID:  "itemID",
   231  					Qty: 3,
   232  				},
   233  				deliveryCode: "deliveryCode",
   234  			},
   235  			want: QuantityAdjustment{
   236  				Item: cart.Item{
   237  					ID:  "itemID",
   238  					Qty: 1,
   239  				},
   240  				DeliveryCode: "deliveryCode",
   241  				PrevQty:      3,
   242  				CurrQty:      1,
   243  				Reason:       "restrictor",
   244  			},
   245  		},
   246  	}
   247  	for _, tt := range tests {
   248  		t.Run(tt.name, func(t *testing.T) {
   249  			gum := &GetQuantityAdjustmentUpdatedItemsMessage{}
   250  			getQuantityAdjustmentUpdatedItemsMessage := gum.Func(tt.args.contextProvider()).(func(cart.Item, string) QuantityAdjustment)
   251  			if got := getQuantityAdjustmentUpdatedItemsMessage(tt.args.item, tt.args.deliveryCode); cmp.Diff(got, tt.want) != "" {
   252  				t.Errorf("Func() = %v, want %v", got, tt.want)
   253  			}
   254  		})
   255  	}
   256  }
   257  
   258  func TestGetQuantityAdjustmentCouponCodesRemoved_Func(t *testing.T) {
   259  	type args struct {
   260  		contextProvider func() context.Context
   261  	}
   262  	tests := []struct {
   263  		name string
   264  		args args
   265  		want bool
   266  	}{
   267  		{
   268  			name: "no adjustments returns false",
   269  			args: args{
   270  				contextProvider: func() context.Context {
   271  					session := web.EmptySession()
   272  
   273  					return web.ContextWithSession(context.Background(), session)
   274  				},
   275  			},
   276  			want: false,
   277  		},
   278  		{
   279  			name: "adjustment that has not removed coupon codes returns false",
   280  			args: args{
   281  				contextProvider: func() context.Context {
   282  					session := web.EmptySession()
   283  					session.Store("cart.view.quantity.adjustments", application.QtyAdjustmentResults{
   284  						{
   285  							HasRemovedCouponCodes: false,
   286  						},
   287  					})
   288  
   289  					return web.ContextWithSession(context.Background(), session)
   290  				},
   291  			},
   292  			want: false,
   293  		},
   294  		{
   295  			name: "adjustment that has removed coupon codes returns true",
   296  			args: args{
   297  				contextProvider: func() context.Context {
   298  					session := web.EmptySession()
   299  					session.Store("cart.view.quantity.adjustments", application.QtyAdjustmentResults{
   300  						{
   301  							HasRemovedCouponCodes: true,
   302  						},
   303  					})
   304  
   305  					return web.ContextWithSession(context.Background(), session)
   306  				},
   307  			},
   308  			want: true,
   309  		},
   310  		{
   311  			name: "at least one adjustment that has removed coupon codes returns true",
   312  			args: args{
   313  				contextProvider: func() context.Context {
   314  					session := web.EmptySession()
   315  					session.Store("cart.view.quantity.adjustments", application.QtyAdjustmentResults{
   316  						{
   317  							HasRemovedCouponCodes: false,
   318  						},
   319  						{
   320  							HasRemovedCouponCodes: true,
   321  						},
   322  					})
   323  
   324  					return web.ContextWithSession(context.Background(), session)
   325  				},
   326  			},
   327  			want: true,
   328  		},
   329  	}
   330  	for _, tt := range tests {
   331  		t.Run(tt.name, func(t *testing.T) {
   332  			gcd := &GetQuantityAdjustmentCouponCodesRemoved{}
   333  			getQuantityAdjustmentCouponCodesRemoved := gcd.Func(tt.args.contextProvider()).(func() bool)
   334  			if got := getQuantityAdjustmentCouponCodesRemoved(); cmp.Diff(got, tt.want) != "" {
   335  				t.Errorf("Func() = %v, want %v", got, tt.want)
   336  			}
   337  		})
   338  	}
   339  }
   340  
   341  func TestRemoveQuantityAdjustmentMessages_Func(t *testing.T) {
   342  	session := web.EmptySession()
   343  	session.Store("cart.view.quantity.adjustments", application.QtyAdjustmentResults{{}})
   344  
   345  	ctx := web.ContextWithSession(context.Background(), session)
   346  
   347  	rm := &RemoveQuantityAdjustmentMessages{}
   348  	removeQuantityAdjustmentMessages := rm.Func(ctx).(func() bool)
   349  
   350  	_, found := session.Load("cart.view.quantity.adjustments")
   351  	assert.Equal(t, true, found)
   352  
   353  	removeQuantityAdjustmentMessages()
   354  
   355  	_, found = session.Load("cart.view.quantity.adjustments")
   356  	assert.Equal(t, false, found)
   357  }