pagePopupLock.test.mjs 2.05 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import {
  computePagePopupVisible,
  shouldSyncPagePopup,
  closePagePopupPatch,
  mergeSetDataForPopupLock
} from '../services/pagePopupLock.js'

describe('pagePopupLock', () => {
  it('computePagePopupVisible is false when no popup open', () => {
    assert.equal(computePagePopupVisible({ customerInfoBox: false }), false)
  })

  it('computePagePopupVisible is true when any popup open', () => {
    assert.equal(computePagePopupVisible({ customerInfoBox: true }), true)
  })

  it('shouldSyncPagePopup detects popup field updates', () => {
    assert.equal(shouldSyncPagePopup({ customerInfoBox: true }), true)
    assert.equal(shouldSyncPagePopup({ orderObj: {} }), false)
    assert.equal(shouldSyncPagePopup({ pagePopupVisible: true }), false)
  })

  it('closePagePopupPatch returns false patch for popup key', () => {
    assert.deepEqual(closePagePopupPatch('customerInfoBox'), {
      customerInfoBox: false
    })
    assert.deepEqual(closePagePopupPatch(''), {})
    assert.deepEqual(closePagePopupPatch(null), {})
  })

  it('wrapper scenario: closing last popup sets pagePopupVisible false', () => {
    const data = { customerInfoBox: true, goodsBox: false }
    const updates = closePagePopupPatch('customerInfoBox')
    assert.equal(shouldSyncPagePopup(updates), true)
    const merged = mergeSetDataForPopupLock(data, updates)
    assert.equal(computePagePopupVisible(merged), false)
  })

  it('closing subShopMerchantBox unlocks page when no other popup open', () => {
    const data = { subShopMerchantBox: true, customerInfoBox: false }
    const merged = mergeSetDataForPopupLock(
      data,
      closePagePopupPatch('subShopMerchantBox')
    )
    assert.equal(computePagePopupVisible(merged), false)
  })

  it('closing skuGoodsBox unlocks page when no other popup open', () => {
    const data = { skuGoodsBox: true, goodsBox: false }
    const merged = mergeSetDataForPopupLock(data, closePagePopupPatch('skuGoodsBox'))
    assert.equal(computePagePopupVisible(merged), false)
  })
})