local V=require("\046\046\047\108\111\111\112\087\104\105\108\101")local h=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 G=h.expect local Q=h.jest local R=h.it local A=h.beforeEach local i local k A(function()k,i=Q.fn()end)R("\100\111\101\115 \110\111\116 \114\117\110 \116\104\101 \102\117\110\099\116\105\111\110 \105\102 \105\116 \105\115 \099\097\110\099\101\108\108\101\100 \105\109\109\101\100\105\097\116\101\108\121",function()k.mockReturnValue(true)local h=V(1,i)h();(G(k)).never.toHaveBeenCalled()end)R("\114\117\110\115 \116\104\101 \102\117\110\099\116\105\111\110 \111\110\099\101 \105\102 \105\116 \114\101\116\117\114\110\115 \102\097\108\115\101 \116\104\101 \102\105\114\115\116 \116\105\109\101",function()k.mockReturnValueOnce(false)local h=V(0,i)task.wait();(G(k)).toHaveBeenCalledTimes(1);(G(k)).toHaveBeenCalledWith(G.any("\110\117\109\098\101\114"))end)R("\114\117\110\115 \116\104\101 \102\117\110\099\116\105\111\110 \116\119\105\099\101 \105\102 \105\116 \114\101\116\117\114\110\115 \102\097\108\115\101 \116\104\101 \115\101\099\111\110\100 \116\105\109\101",function()k.mockReturnValueOnce(true)k.mockReturnValueOnce(false)local h=V(0,i)task.wait()task.wait();(G(k)).toHaveBeenCalledTimes(2);(G(k)).toHaveBeenCalledWith(G.any("\110\117\109\098\101\114"))end)
local loopWhile = require('../loopWhile')

local jestGlobals = require('@pkg/@jsdotlua/jest-globals')

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

local fn
local fnMock

beforeEach(function()
    fnMock, fn = jest.fn()
end)

it('does not run the function if it is cancelled immediately', function()
    fnMock.mockReturnValue(true)
    local cancel = loopWhile(1, fn)
    cancel()
    expect(fnMock).never.toHaveBeenCalled()
end)

it('runs the function once if it returns false the first time', function()
    fnMock.mockReturnValueOnce(false)
    local _cancel = loopWhile(0, fn)
    task.wait()
    expect(fnMock).toHaveBeenCalledTimes(1)
    expect(fnMock).toHaveBeenCalledWith(expect.any('number'))
end)

it('runs the function twice if it returns false the second time', function()
    fnMock.mockReturnValueOnce(true)
    fnMock.mockReturnValueOnce(false)
    local _cancel = loopWhile(0, fn)
    task.wait()
    task.wait()
    expect(fnMock).toHaveBeenCalledTimes(2)
    expect(fnMock).toHaveBeenCalledWith(expect.any('number'))
end)
