import { History } from "../../partial-src/component/history"
export function run() {
    const interval = 500
    describe("Test history", () => {
        it("Test history", (done: Function) => {
            let his = new History()
            if (his.state.state !== "waiting") throw new Error("Invalid initial state")
            let end = false
            let end2 = false
            his.registerBackButton("popup2", () => {
                end2 = true
            })
            his.registerBackButton("popup", () => {
                end = true
            })
            window.history.back()
            setTimeout(() => {
                if (!end) {
                    throw new Error("Invalid popstate")
                }
                window.history.back()
                setTimeout(() => {
                    if (!end2) {
                        throw new Error("Invalid multi popup")
                    }
                    done()
                }, interval)
            }, interval)
        })
        it("Test goto", (done: Function) => {
            let his = new History()
            let wheres: string[] = []
            his.events.listenBy(this, "goto", (where) => {
                wheres.push(where)
            })
            let key = Math.random().toString().slice(4, 10)
            his.goto(`/${key}`)
            setTimeout(() => {
                window.history.back()
                setTimeout(() => {
                    if (wheres.length != 2) {
                        done(new Error("Invalid goto"))
                        return
                    }
                    if (wheres[0].indexOf(key) < 0) {
                        done(new Error("Invalid goto"))
                        return
                    }
                    done()
                }, interval)
            }, interval)
        })
    })
}
