4
0

prefilledArguments.test.mjs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import test from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import { getInitialArgumentValue, readPrefilledArgumentsFromNavigation } from './prefilledArguments.js'
  4. test('readPrefilledArgumentsFromNavigation returns navigation state values', () => {
  5. const originalState = window.history.state
  6. window.history.replaceState({ prefilledArguments: { ansible_host: '10.0.0.1' } }, '')
  7. assert.deepEqual(readPrefilledArgumentsFromNavigation(), { ansible_host: '10.0.0.1' })
  8. window.history.replaceState(originalState, '')
  9. })
  10. test('getInitialArgumentValue prefers navigation state over query params', () => {
  11. const originalState = window.history.state
  12. const originalSearch = window.location.search
  13. window.history.replaceState({ prefilledArguments: { ansible_host: '10.0.0.1' } }, '')
  14. window.history.replaceState(window.history.state, '', '?ansible_host=10.0.0.2')
  15. assert.equal(getInitialArgumentValue('ansible_host', readPrefilledArgumentsFromNavigation()), '10.0.0.1')
  16. window.history.replaceState(originalState, '')
  17. window.history.replaceState(window.history.state, '', originalSearch || '/')
  18. })
  19. test('getInitialArgumentValue falls back to query params when state is absent', () => {
  20. const originalState = window.history.state
  21. const originalSearch = window.location.search
  22. window.history.replaceState({}, '')
  23. window.history.replaceState(window.history.state, '', '?ansible_host=10.0.0.2')
  24. assert.equal(getInitialArgumentValue('ansible_host', readPrefilledArgumentsFromNavigation()), '10.0.0.2')
  25. window.history.replaceState(originalState, '')
  26. window.history.replaceState(window.history.state, '', originalSearch || '/')
  27. })