{"id":2570,"date":"2019-09-11T13:22:01","date_gmt":"2019-09-11T12:22:01","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=2570"},"modified":"2019-09-11T13:22:29","modified_gmt":"2019-09-11T12:22:29","slug":"typescript-jest-unit-test-example","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/typescript-jest-unit-test-example\/","title":{"rendered":"Typescript Jest Unit Test Example"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ AttentionDayService.spec.ts\nimport { AttentionDayService } from \".\/AttentionDayService\";\nimport { TestWiring } from \"..\/..\/wiring\/TestWiring\";\nimport { IAttentionDayRepository } from \"..\/ports\/secondary\/IAttentionDayRepository\";\nimport { IAttentionDay, KindOfAttentionDay } from \"..\/models\/IAttentionDay\";\nimport { ValidationError } from \"..\/models\/ErrorTypes\/ValidationError\";\n\ndescribe(\"AttentionDayService\", () => {\n    let attentionDayRepository: IAttentionDayRepository;\n    let attentionDayService: AttentionDayService;\n\n    beforeAll(() => {\n        const wiring = new TestWiring();\n        const resolver = wiring.portResolver;\n        attentionDayRepository = {} as IAttentionDayRepository;\n        resolver.registerInstance(\"IAttentionDayRepository\", () => attentionDayRepository);\n        attentionDayService = new AttentionDayService()\n    });\n\n    it(\"should call getById() from requestById()\", async () => {\n        const attentionDay = {} as IAttentionDay;\n        attentionDayRepository.getById = jest.fn(_ => Promise.resolve(attentionDay));\n        const result = attentionDayService.requestById(\"abc\");\n        expect(attentionDayRepository.getById).toHaveBeenCalledTimes(1);\n        expect(attentionDayRepository.getById).toHaveBeenCalledWith(\"abc\");\n        await expect(result).resolves.toBe(attentionDay);\n    })\n\n    it(\"should call updateAttentionDay() from requestUpdateAttentionDay()\", async () => {\n        const attentionDay = {\n            id: 12,\n            name: \"Fake\",\n            start: new Date(2011, 1, 1),\n            end: new Date(2011, 1, 1),\n            kind: KindOfAttentionDay.AttentionDay\n        } as IAttentionDay;\n        attentionDayRepository.updateAttentionDay = jest.fn(_ => Promise.resolve(attentionDay));\n        const result = attentionDayService.requestUpdateAttentionDay(attentionDay);\n        expect(attentionDayRepository.updateAttentionDay).toHaveBeenCalledTimes(1);\n        expect(attentionDayRepository.updateAttentionDay).toHaveBeenCalledWith(attentionDay);\n        await expect(result).resolves.toBe(attentionDay);\n    })\n\n    it(\"should call updateAttentionDay() from requestUpdateAttentionDay()\", async () => {\n        const attentionDay = {\n            id: 12,\n            name: \"InvalidAttentionDay\",\n            start: new Date(2011, 2, 1),\n            end: new Date(2011, 1, 1),\n            kind: KindOfAttentionDay.AttentionDay\n        } as IAttentionDay;\n        attentionDayRepository.updateAttentionDay = jest.fn(_ => Promise.resolve(attentionDay));\n        await expect(attentionDayService.requestUpdateAttentionDay(attentionDay)).rejects.toThrow(ValidationError);\n        expect(attentionDayRepository.updateAttentionDay).toHaveBeenCalledTimes(0);\n    })\n});\n\nexport {};\n<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2570","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2570","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=2570"}],"version-history":[{"count":2,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2570\/revisions"}],"predecessor-version":[{"id":2572,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/2570\/revisions\/2572"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=2570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=2570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=2570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}