11import { FallbackTreatmentsCalculator } from '../' ;
22import type { FallbackTreatmentConfiguration } from '../../../../types/splitio' ;
3+ import { loggerMock } from '../../../logger/__tests__/sdkLogger.mock' ;
34import { CONTROL } from '../../../utils/constants' ;
45
5- describe ( 'FallbackTreatmentsCalculator' , ( ) => {
6- const spy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
6+ describe ( 'FallbackTreatmentsCalculator' , ( ) => {
77 const longName = 'a' . repeat ( 101 ) ;
88
99 test ( 'logs an error if flag name is invalid - by Flag' , ( ) => {
@@ -12,17 +12,17 @@ describe('FallbackTreatmentsCalculator', () => {
1212 'feature A' : { treatment : 'TREATMENT_A' , config : '{ value: 1 }' } ,
1313 } ,
1414 } ;
15- new FallbackTreatmentsCalculator ( config ) ;
16- expect ( spy . mock . calls [ 0 ] [ 0 ] ) . toBe (
15+ new FallbackTreatmentsCalculator ( loggerMock , config ) ;
16+ expect ( loggerMock . error . mock . calls [ 0 ] [ 0 ] ) . toBe (
1717 'Fallback treatments - Discarded flag \'feature A\': Invalid flag name (max 100 chars, no spaces)'
1818 ) ;
1919 config = {
2020 byFlag : {
2121 [ longName ] : { treatment : 'TREATMENT_A' , config : '{ value: 1 }' } ,
2222 } ,
2323 } ;
24- new FallbackTreatmentsCalculator ( config ) ;
25- expect ( spy . mock . calls [ 1 ] [ 0 ] ) . toBe (
24+ new FallbackTreatmentsCalculator ( loggerMock , config ) ;
25+ expect ( loggerMock . error . mock . calls [ 1 ] [ 0 ] ) . toBe (
2626 `Fallback treatments - Discarded flag '${ longName } ': Invalid flag name (max 100 chars, no spaces)`
2727 ) ;
2828
@@ -31,8 +31,8 @@ describe('FallbackTreatmentsCalculator', () => {
3131 'featureB' : { treatment : longName , config : '{ value: 1 }' } ,
3232 } ,
3333 } ;
34- new FallbackTreatmentsCalculator ( config ) ;
35- expect ( spy . mock . calls [ 2 ] [ 0 ] ) . toBe (
34+ new FallbackTreatmentsCalculator ( loggerMock , config ) ;
35+ expect ( loggerMock . error . mock . calls [ 2 ] [ 0 ] ) . toBe (
3636 'Fallback treatments - Discarded treatment for flag \'featureB\': Invalid treatment (max 100 chars and must match pattern)'
3737 ) ;
3838
@@ -42,8 +42,8 @@ describe('FallbackTreatmentsCalculator', () => {
4242 'featureC' : { config : '{ global: true }' } ,
4343 } ,
4444 } ;
45- new FallbackTreatmentsCalculator ( config ) ;
46- expect ( spy . mock . calls [ 3 ] [ 0 ] ) . toBe (
45+ new FallbackTreatmentsCalculator ( loggerMock , config ) ;
46+ expect ( loggerMock . error . mock . calls [ 3 ] [ 0 ] ) . toBe (
4747 'Fallback treatments - Discarded treatment for flag \'featureC\': Invalid treatment (max 100 chars and must match pattern)'
4848 ) ;
4949
@@ -53,8 +53,8 @@ describe('FallbackTreatmentsCalculator', () => {
5353 'featureC' : { treatment : 'invalid treatment!' , config : '{ global: true }' } ,
5454 } ,
5555 } ;
56- new FallbackTreatmentsCalculator ( config ) ;
57- expect ( spy . mock . calls [ 4 ] [ 0 ] ) . toBe (
56+ new FallbackTreatmentsCalculator ( loggerMock , config ) ;
57+ expect ( loggerMock . error . mock . calls [ 4 ] [ 0 ] ) . toBe (
5858 'Fallback treatments - Discarded treatment for flag \'featureC\': Invalid treatment (max 100 chars and must match pattern)'
5959 ) ;
6060 } ) ;
@@ -63,26 +63,26 @@ describe('FallbackTreatmentsCalculator', () => {
6363 let config : FallbackTreatmentConfiguration = {
6464 global : { treatment : longName , config : '{ value: 1 }' } ,
6565 } ;
66- new FallbackTreatmentsCalculator ( config ) ;
67- expect ( spy . mock . calls [ 2 ] [ 0 ] ) . toBe (
66+ new FallbackTreatmentsCalculator ( loggerMock , config ) ;
67+ expect ( loggerMock . error . mock . calls [ 2 ] [ 0 ] ) . toBe (
6868 'Fallback treatments - Discarded treatment for flag \'featureB\': Invalid treatment (max 100 chars and must match pattern)'
6969 ) ;
7070
7171 config = {
7272 // @ts -ignore
7373 global : { config : '{ global: true }' } ,
7474 } ;
75- new FallbackTreatmentsCalculator ( config ) ;
76- expect ( spy . mock . calls [ 3 ] [ 0 ] ) . toBe (
75+ new FallbackTreatmentsCalculator ( loggerMock , config ) ;
76+ expect ( loggerMock . error . mock . calls [ 3 ] [ 0 ] ) . toBe (
7777 'Fallback treatments - Discarded treatment for flag \'featureC\': Invalid treatment (max 100 chars and must match pattern)'
7878 ) ;
7979
8080 config = {
8181 // @ts -ignore
8282 global : { treatment : 'invalid treatment!' , config : '{ global: true }' } ,
8383 } ;
84- new FallbackTreatmentsCalculator ( config ) ;
85- expect ( spy . mock . calls [ 4 ] [ 0 ] ) . toBe (
84+ new FallbackTreatmentsCalculator ( loggerMock , config ) ;
85+ expect ( loggerMock . error . mock . calls [ 4 ] [ 0 ] ) . toBe (
8686 'Fallback treatments - Discarded treatment for flag \'featureC\': Invalid treatment (max 100 chars and must match pattern)'
8787 ) ;
8888 } ) ;
@@ -93,7 +93,7 @@ describe('FallbackTreatmentsCalculator', () => {
9393 'featureA' : { treatment : 'TREATMENT_A' , config : '{ value: 1 }' } ,
9494 } ,
9595 } ;
96- const calculator = new FallbackTreatmentsCalculator ( config ) ;
96+ const calculator = new FallbackTreatmentsCalculator ( loggerMock , config ) ;
9797 const result = calculator . resolve ( 'featureA' , 'label by flag' ) ;
9898
9999 expect ( result ) . toEqual ( {
@@ -108,7 +108,7 @@ describe('FallbackTreatmentsCalculator', () => {
108108 byFlag : { } ,
109109 global : { treatment : 'GLOBAL_TREATMENT' , config : '{ global: true }' } ,
110110 } ;
111- const calculator = new FallbackTreatmentsCalculator ( config ) ;
111+ const calculator = new FallbackTreatmentsCalculator ( loggerMock , config ) ;
112112 const result = calculator . resolve ( 'missingFlag' , 'label by global' ) ;
113113
114114 expect ( result ) . toEqual ( {
@@ -122,7 +122,7 @@ describe('FallbackTreatmentsCalculator', () => {
122122 const config : FallbackTreatmentConfiguration = {
123123 byFlag : { } ,
124124 } ;
125- const calculator = new FallbackTreatmentsCalculator ( config ) ;
125+ const calculator = new FallbackTreatmentsCalculator ( loggerMock , config ) ;
126126 const result = calculator . resolve ( 'missingFlag' , 'label by noFallback' ) ;
127127
128128 expect ( result ) . toEqual ( {
@@ -135,15 +135,15 @@ describe('FallbackTreatmentsCalculator', () => {
135135 test ( 'returns undefined label if no label provided' , ( ) => {
136136 const config : FallbackTreatmentConfiguration = {
137137 byFlag : {
138- 'featureB' : { treatment : 'TREATMENT_B' } ,
138+ 'featureB' : { treatment : 'TREATMENT_B' , config : '{ value: 1 }' } ,
139139 } ,
140140 } ;
141- const calculator = new FallbackTreatmentsCalculator ( config ) ;
141+ const calculator = new FallbackTreatmentsCalculator ( loggerMock , config ) ;
142142 const result = calculator . resolve ( 'featureB' ) ;
143143
144144 expect ( result ) . toEqual ( {
145145 treatment : 'TREATMENT_B' ,
146- config : undefined ,
146+ config : '{ value: 1 }' ,
147147 label : '' ,
148148 } ) ;
149149 } ) ;
0 commit comments