|
| 1 | +import "reflect-metadata"; |
| 2 | +import {MetadataBuilder} from "../../src/metadata-builder/MetadataBuilder"; |
| 3 | + |
| 4 | +import {Post} from "../../src/decorator/Post"; |
| 5 | +import {Controller} from "../../src"; |
| 6 | + |
| 7 | +const expect = require("chakram").expect; |
| 8 | + |
| 9 | +describe("controller inheritance", () => { |
| 10 | + const metadataBuilder = new MetadataBuilder({}); |
| 11 | + |
| 12 | + abstract class AbstractControllerTemplate { |
| 13 | + @Post() |
| 14 | + public create() { |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + @Controller(`/derivative`) |
| 19 | + class DerivativeController extends AbstractControllerTemplate { |
| 20 | + } |
| 21 | + |
| 22 | + @Controller(`/autonomous`) |
| 23 | + class AutonomousController { |
| 24 | + @Post() |
| 25 | + public create() { |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + |
| 30 | + it("should build empty meta for empty set", () => { |
| 31 | + const meta = metadataBuilder.buildControllerMetadata([]); |
| 32 | + |
| 33 | + expect(meta.length).to.be.eq(0); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should build meta if only derivative controller given", () => { |
| 37 | + const meta = metadataBuilder.buildControllerMetadata([ |
| 38 | + DerivativeController, |
| 39 | + ]); |
| 40 | + |
| 41 | + expect(meta.length).to.be.eq(1); |
| 42 | + expect(meta[0].route).to.be.eq("/derivative"); |
| 43 | + expect(meta[0].actions.length).to.be.eq(1); |
| 44 | + |
| 45 | + expect(meta[0].actions[0].method).to.be.eq("create"); |
| 46 | + expect(meta[0].actions[0].type).to.be.eq("post"); |
| 47 | + }); |
| 48 | + |
| 49 | + it("should build meta if only autonomous controller given", () => { |
| 50 | + const meta = metadataBuilder.buildControllerMetadata([ |
| 51 | + AutonomousController, |
| 52 | + ]); |
| 53 | + |
| 54 | + expect(meta.length).to.be.eq(1); |
| 55 | + expect(meta[0].route).to.be.eq("/autonomous"); |
| 56 | + expect(meta[0].actions.length).to.be.eq(1); |
| 57 | + |
| 58 | + expect(meta[0].actions[0].method).to.be.eq("create"); |
| 59 | + expect(meta[0].actions[0].type).to.be.eq("post"); |
| 60 | + }); |
| 61 | + |
| 62 | + it("should build meta if autonomous and derivative controllers given", () => { |
| 63 | + const meta = metadataBuilder.buildControllerMetadata([ |
| 64 | + DerivativeController, |
| 65 | + AutonomousController, |
| 66 | + ]); |
| 67 | + |
| 68 | + expect(meta.length).to.be.eq(2); |
| 69 | + expect(meta[0].actions.length).to.be.eq(1); |
| 70 | + expect(meta[1].actions.length).to.be.eq(1); |
| 71 | + |
| 72 | + expect(meta[0].actions[0].method).to.be.eq("create"); |
| 73 | + expect(meta[0].actions[0].type).to.be.eq("post"); |
| 74 | + expect(meta[1].actions[0].method).to.be.eq("create"); |
| 75 | + expect(meta[1].actions[0].type).to.be.eq("post"); |
| 76 | + }); |
| 77 | + |
| 78 | +}); |
0 commit comments