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("\046\046\047\117\115\101\085\110\109\111\117\110\116")local A=f.expect local V=f.it local m=f.jest V("\099\097\108\108\115 \116\104\101 \112\114\111\118\105\100\101\100 \099\108\101\097\110\117\112 \102\117\110\099\116\105\111\110 \111\110 \117\110\109\111\117\110\116",function()local f=m.fn()local V=F(l,f)V.unmount();(A(f)).toHaveBeenCalledTimes(1)end)V("\100\111\101\115 \110\111\116 \099\097\108\108 \116\104\101 \099\108\101\097\110\117\112 \102\117\110\099\116\105\111\110 \111\110 \114\101\114\101\110\100\101\114",function()local f=m.fn()local V=F(l,f)V.rerender(f);(A(f)).toHaveBeenCalledTimes(0)end)V("\099\097\108\108\115 \116\104\101 \108\097\115\116 \099\108\101\097\110\117\112 \102\117\110\099\116\105\111\110 \111\110 \117\110\109\111\117\110\116",function()local f=m.fn()local V=m.fn()local e=F(l,f)e.rerender(V)e.unmount();(A(f)).toHaveBeenCalledTimes(0);(A(V)).toHaveBeenCalledTimes(1)end)V("\100\111\101\115 \110\111\116 \116\104\114\111\119 \105\102 \110\111 \102\117\110\099\116\105\111\110 \105\115 \112\114\111\118\105\100\101\100",function()local f=F(l);(A(function()f.unmount()end)).never.toThrow()end)
local jestGlobals = require('@pkg/@jsdotlua/jest-globals')
local renderHook = require('@pkg/@seaofvoices/react-render-hook').renderHook

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

local expect = jestGlobals.expect
local it = jestGlobals.it
local jest = jestGlobals.jest

it('calls the provided cleanup function on unmount', function()
    local cleanupMock = jest.fn()

    local renderResult = renderHook(useUnmount, cleanupMock)

    renderResult.unmount()

    expect(cleanupMock).toHaveBeenCalledTimes(1)
end)

it('does not call the cleanup function on rerender', function()
    local cleanupMock = jest.fn()

    local renderResult = renderHook(useUnmount, cleanupMock)

    renderResult.rerender(cleanupMock)

    expect(cleanupMock).toHaveBeenCalledTimes(0)
end)

it('calls the last cleanup function on unmount', function()
    local cleanupMock = jest.fn()
    local lastCleanupMock = jest.fn()

    local renderResult = renderHook(useUnmount, cleanupMock)

    renderResult.rerender(lastCleanupMock)

    renderResult.unmount()

    expect(cleanupMock).toHaveBeenCalledTimes(0)
    expect(lastCleanupMock).toHaveBeenCalledTimes(1)
end)

it('does not throw if no function is provided', function()
    local renderResult = renderHook(useUnmount)

    expect(function()
        renderResult.unmount()
    end).never.toThrow()
end)
