local f=require("\064\112\107\103\047\064\106\115\100\111\116\108\117\097\047\106\101\115\116-\103\108\111\098\097\108\115")local F=(require("\064\112\107\103\047\064\115\101\097\111\102\118\111\105\099\101\115\047\114\101\097\099\116-\114\101\110\100\101\114-\104\111\111\107")).renderHook local l=require("\064\112\107\103\047\064\106\115\100\111\116\108\117\097\047\114\101\097\099\116-\116\101\115\116\105\110\103-\108\105\098\114\097\114\121")local A=require("\046\046\047\117\115\101\068\101\102\097\117\108\116\083\116\097\116\101")local V=f.expect local m=f.it local e=l.act m("\114\101\116\117\114\110\115 \116\104\101 \100\101\102\097\117\108\116 \118\097\108\117\101 \119\104\101\110 \116\104\101 \118\097\108\117\101 \105\115 \110\105\108",function()local f=F(function()return A(nil,"\100\101\102\097\117\108\116")end);(V(f.result.current[1])).toBe("\100\101\102\097\117\108\116")end)m("\114\101\116\117\114\110\115 \116\104\101 \118\097\108\117\101 \119\104\101\110 \105\116 \105\115 \110\111\116 \110\105\108",function()local f=F(function()return A("\105\110\105\116\105\097\108","\100\101\102\097\117\108\116")end);(V(f.result.current[1])).toBe("\105\110\105\116\105\097\108")end)m("\117\112\100\097\116\101\115 \116\104\101 \118\097\108\117\101 \099\111\114\114\101\099\116\108\121 \119\105\116\104 \115\101\116\083\116\097\116\101",function()local f=F(function()return A(nil,"\100\101\102\097\117\108\116")end)local l=f.result.current[2]e(function()l("\117\112\100\097\116\101\100")end);(V(f.result.current[1])).toBe("\117\112\100\097\116\101\100")end)m("\114\101\116\117\114\110\115 \116\104\101 \100\101\102\097\117\108\116 \118\097\108\117\101 \119\104\101\110 \115\101\116\083\116\097\116\101 \105\115 \099\097\108\108\101\100 \119\105\116\104 \110\105\108",function()local f=F(function()return A(nil,"\100\101\102\097\117\108\116")end)local l=f.result.current[2]e(function()l(nil)end);(V(f.result.current[1])).toBe("\100\101\102\097\117\108\116")end)
local jestGlobals = require('@pkg/@jsdotlua/jest-globals')
local renderHook = require('@pkg/@seaofvoices/react-render-hook').renderHook
local ReactTesting = require('@pkg/@jsdotlua/react-testing-library')

local useDefaultState = require('../useDefaultState')

local expect = jestGlobals.expect
local it = jestGlobals.it
local act = ReactTesting.act

it('returns the default value when the value is nil', function()
    local renderResult = renderHook(function()
        return useDefaultState(nil, 'default')
    end)

    expect(renderResult.result.current[1]).toBe('default')
end)

it('returns the value when it is not nil', function()
    local renderResult = renderHook(function()
        return useDefaultState('initial', 'default')
    end)

    expect(renderResult.result.current[1]).toBe('initial')
end)

it('updates the value correctly with setState', function()
    local renderResult = renderHook(function()
        return useDefaultState(nil, 'default')
    end)

    local setValue = renderResult.result.current[2]

    act(function()
        setValue('updated')
    end)

    expect(renderResult.result.current[1]).toBe('updated')
end)

it('returns the default value when setState is called with nil', function()
    local renderResult = renderHook(function()
        return useDefaultState(nil, 'default')
    end)

    local setValue = renderResult.result.current[2]

    act(function()
        setValue(nil)
    end)

    expect(renderResult.result.current[1]).toBe('default')
end)
