{"size": 5015, "ext": "js", "max_stars_repo_path": "spec/Editor/JavaScriptCompilerWorker.js", "max_stars_repo_name": "jameswilddev/influx8", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "spec/Editor/JavaScriptCompilerWorker.js", "max_issues_repo_name": "jameswilddev/influx8", "max_issues_repo_issues_event_min_datetime": "2017-04-22T05:44:36.000Z", "max_issues_repo_issues_event_max_datetime": "2017-06-17T08:06:32.000Z", "max_forks_repo_path": "spec/Editor/JavaScriptCompilerWorker.js", "max_forks_repo_name": "jameswilddev/influx8", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 17, "max_forks_count": null, "avg_line_length": 56.9886363636, "max_line_length": 165, "alphanum_fraction": 0.6269192423}
describe("JavaScriptCompilerWorker", () => {
    const rewire = require("rewire")
    let namespace
    let matchCSyntax
    let generateCSyntax
    beforeEach(() => {
        global.addEventListener = jasmine.createSpy("addEventListener")
        global.postMessage = jasmine.createSpy("postMessage")

        namespace = rewire("../../Editor.JavaScriptCompilerWorker.js")

        matchCSyntax = jasmine.createSpy("MatchCSyntax")
        namespace.__set__("MatchCSyntax", matchCSyntax)

        generateCSyntax = jasmine.createSpy("GenerateCSyntax")
        namespace.__set__("GenerateCSyntax", generateCSyntax)

        namespace.__set__("JavaScriptCSyntax", "test javascript c syntax")
    })
    afterEach(() => {
        delete global.addEventListener
        delete global.postMessage
    })
    it("adds one event listener", () => expect(global.addEventListener.calls.count()).toEqual(1))
    it("listens for messages", () => expect(global.addEventListener).toHaveBeenCalledWith("message", jasmine.any(Function)))
    it("does not post a message", () => expect(global.postMessage).not.toHaveBeenCalled())
    it("does not match C syntax", () => expect(matchCSyntax).not.toHaveBeenCalled())
    it("does not generate C syntax", () => expect(generateCSyntax).not.toHaveBeenCalled())

    describe("on receiving a message without a verified expression", () => {
        beforeEach(() => {
            global.addEventListener.calls.argsFor(0)[1]({
                data: {
                    BuildId: "test build id",
                    Typechecked: "test typechecked",
                    Verified: undefined
                }
            })
        })
        it("does not add further event listeners", () => expect(global.addEventListener.calls.count()).toEqual(1))
        it("does not match C syntax", () => expect(matchCSyntax).not.toHaveBeenCalled())
        it("does not generate C syntax", () => expect(generateCSyntax).not.toHaveBeenCalled())
        it("posts one message", () => expect(global.postMessage.calls.count()).toEqual(1))
        it("posts the build id", () => expect(global.postMessage.calls.argsFor(0)[0].BuildId).toEqual("test build id"))
        it("posts undefined data", () => expect(global.postMessage.calls.argsFor(0)[0].Data).toBeUndefined())
    })

    describe("on receiving a message with a verified expression which does not match", () => {
        beforeEach(() => {
            global.addEventListener.calls.argsFor(0)[1]({
                data: {
                    BuildId: "test build id",
                    Typechecked: "test typechecked",
                    Verified: "test verified"
                }
            })
        })
        it("does not add further event listeners", () => expect(global.addEventListener.calls.count()).toEqual(1))
        it("matches one expression and C syntax", () => expect(matchCSyntax.calls.count()).toEqual(1))
        it("matches the verified expression and JavaScript C syntax", () => expect(matchCSyntax).toHaveBeenCalledWith("test verified", "test javascript c syntax"))
        it("does not generate C syntax", () => expect(generateCSyntax).not.toHaveBeenCalled())
        it("posts one message", () => expect(global.postMessage.calls.count()).toEqual(1))
        it("posts the build id", () => expect(global.postMessage.calls.argsFor(0)[0].BuildId).toEqual("test build id"))
        it("posts undefined data", () => expect(global.postMessage.calls.argsFor(0)[0].Data).toBeUndefined())
    })

    describe("on receiving a message with a verified expression which matches", () => {
        beforeEach(() => {
            matchCSyntax.and.returnValue("test matched c syntax")
            generateCSyntax.and.returnValue("test generated c syntax")
            global.addEventListener.calls.argsFor(0)[1]({
                data: {
                    BuildId: "test build id",
                    Typechecked: "test typechecked",
                    Verified: "test verified"
                }
            })
        })
        it("does not add further event listeners", () => expect(global.addEventListener.calls.count()).toEqual(1))
        it("matches one expression and C syntax", () => expect(matchCSyntax.calls.count()).toEqual(1))
        it("matches the verified expression and JavaScript C syntax", () => expect(matchCSyntax).toHaveBeenCalledWith("test verified", "test javascript c syntax"))
        it("generates one C syntax", () => expect(generateCSyntax.calls.count()).toEqual(1))
        it("generates C syntax from the matched expression", () => expect(generateCSyntax).toHaveBeenCalledWith("test matched c syntax", "test javascript c syntax"))
        it("posts one message", () => expect(global.postMessage.calls.count()).toEqual(1))
        it("posts the build id", () => expect(global.postMessage.calls.argsFor(0)[0].BuildId).toEqual("test build id"))
        it("posts the generated C syntax as data", () => expect(global.postMessage.calls.argsFor(0)[0].Data).toEqual("test generated c syntax"))
    })
})