@@ -4,9 +4,6 @@ import createStandaloneVariantsSync, {
44} from '../src/standalone-variants/standalone-variants' ;
55import {
66 convertAttributeToUpdateActionShape ,
7- buildPublishAction ,
8- buildUnpublishAction ,
9- buildRemoveStagedChangesAction ,
107 StandaloneVariant ,
118} from '../src/standalone-variants/standalone-variant-actions' ;
129import { SyncAction } from '../src/utils/types' ;
@@ -1197,25 +1194,149 @@ describe('Actions', () => {
11971194 } ) ;
11981195} ) ;
11991196
1200- describe ( 'Lifecycle actions' , ( ) => {
1201- describe ( 'buildPublishAction' , ( ) => {
1202- test ( 'should build publish action' , ( ) => {
1203- const action = buildPublishAction ( ) ;
1204- expect ( action ) . toEqual ( { action : 'publish' } ) ;
1205- } ) ;
1197+ describe ( 'Publish/Unpublish actions' , ( ) => {
1198+ let standaloneVariantSync : SyncAction <
1199+ StandaloneVariant ,
1200+ StandaloneVariantUpdateAction
1201+ > ;
1202+
1203+ beforeEach ( ( ) => {
1204+ standaloneVariantSync = createStandaloneVariantsSync ( ) ;
12061205 } ) ;
12071206
1208- describe ( 'buildUnpublishAction' , ( ) => {
1209- test ( 'should build unpublish action' , ( ) => {
1210- const action = buildUnpublishAction ( ) ;
1211- expect ( action ) . toEqual ( { action : 'unpublish' } ) ;
1212- } ) ;
1207+ test ( 'should build `publish` action when published changes from false to true' , ( ) => {
1208+ const before : StandaloneVariant = {
1209+ id : '123' ,
1210+ key : 'key-1' ,
1211+ sku : 'sku-1' ,
1212+ published : false ,
1213+ } ;
1214+ const now : StandaloneVariant = {
1215+ id : '123' ,
1216+ key : 'key-1' ,
1217+ sku : 'sku-1' ,
1218+ published : true ,
1219+ } ;
1220+
1221+ const actual = standaloneVariantSync . buildActions ( now , before ) ;
1222+
1223+ expect ( actual ) . toEqual ( [ { action : 'publish' } ] ) ;
12131224 } ) ;
12141225
1215- describe ( 'buildRemoveStagedChangesAction' , ( ) => {
1216- test ( 'should build removeStagedChanges action' , ( ) => {
1217- const action = buildRemoveStagedChangesAction ( ) ;
1218- expect ( action ) . toEqual ( { action : 'removeStagedChanges' } ) ;
1219- } ) ;
1226+ test ( 'should build `publish` action when published changes from undefined to true' , ( ) => {
1227+ const before : StandaloneVariant = {
1228+ id : '123' ,
1229+ key : 'key-1' ,
1230+ sku : 'sku-1' ,
1231+ } ;
1232+ const now : StandaloneVariant = {
1233+ id : '123' ,
1234+ key : 'key-1' ,
1235+ sku : 'sku-1' ,
1236+ published : true ,
1237+ } ;
1238+
1239+ const actual = standaloneVariantSync . buildActions ( now , before ) ;
1240+
1241+ expect ( actual ) . toEqual ( [ { action : 'publish' } ] ) ;
1242+ } ) ;
1243+
1244+ test ( 'should build `unpublish` action when published changes from true to false' , ( ) => {
1245+ const before : StandaloneVariant = {
1246+ id : '123' ,
1247+ key : 'key-1' ,
1248+ sku : 'sku-1' ,
1249+ published : true ,
1250+ } ;
1251+ const now : StandaloneVariant = {
1252+ id : '123' ,
1253+ key : 'key-1' ,
1254+ sku : 'sku-1' ,
1255+ published : false ,
1256+ } ;
1257+
1258+ const actual = standaloneVariantSync . buildActions ( now , before ) ;
1259+
1260+ expect ( actual ) . toEqual ( [ { action : 'unpublish' } ] ) ;
1261+ } ) ;
1262+
1263+ test ( 'should build `unpublish` action when published changes from true to undefined' , ( ) => {
1264+ const before : StandaloneVariant = {
1265+ id : '123' ,
1266+ key : 'key-1' ,
1267+ sku : 'sku-1' ,
1268+ published : true ,
1269+ } ;
1270+ const now : StandaloneVariant = {
1271+ id : '123' ,
1272+ key : 'key-1' ,
1273+ sku : 'sku-1' ,
1274+ } ;
1275+
1276+ const actual = standaloneVariantSync . buildActions ( now , before ) ;
1277+
1278+ expect ( actual ) . toEqual ( [ { action : 'unpublish' } ] ) ;
1279+ } ) ;
1280+
1281+ test ( 'should not build publish/unpublish action when published is unchanged (both true)' , ( ) => {
1282+ const before : StandaloneVariant = {
1283+ id : '123' ,
1284+ key : 'key-1' ,
1285+ sku : 'sku-1' ,
1286+ published : true ,
1287+ } ;
1288+ const now : StandaloneVariant = {
1289+ id : '123' ,
1290+ key : 'key-1' ,
1291+ sku : 'sku-1' ,
1292+ published : true ,
1293+ } ;
1294+
1295+ const actual = standaloneVariantSync . buildActions ( now , before ) ;
1296+
1297+ expect ( actual ) . toEqual ( [ ] ) ;
1298+ } ) ;
1299+
1300+ test ( 'should not build publish/unpublish action when published is unchanged (both false)' , ( ) => {
1301+ const before : StandaloneVariant = {
1302+ id : '123' ,
1303+ key : 'key-1' ,
1304+ sku : 'sku-1' ,
1305+ published : false ,
1306+ } ;
1307+ const now : StandaloneVariant = {
1308+ id : '123' ,
1309+ key : 'key-1' ,
1310+ sku : 'sku-1' ,
1311+ published : false ,
1312+ } ;
1313+
1314+ const actual = standaloneVariantSync . buildActions ( now , before ) ;
1315+
1316+ expect ( actual ) . toEqual ( [ ] ) ;
1317+ } ) ;
1318+
1319+ test ( 'should build publish action along with other changes' , ( ) => {
1320+ const before : StandaloneVariant = {
1321+ id : '123' ,
1322+ key : 'key-1' ,
1323+ sku : 'old-sku' ,
1324+ published : false ,
1325+ } ;
1326+ const now : StandaloneVariant = {
1327+ id : '123' ,
1328+ key : 'key-1' ,
1329+ sku : 'new-sku' ,
1330+ published : true ,
1331+ } ;
1332+
1333+ const actual = standaloneVariantSync . buildActions ( now , before ) ;
1334+
1335+ expect ( actual ) . toEqual (
1336+ expect . arrayContaining ( [
1337+ { action : 'setSku' , sku : 'new-sku' , staged : true } ,
1338+ { action : 'publish' } ,
1339+ ] )
1340+ ) ;
12201341 } ) ;
12211342} ) ;
0 commit comments