Skip to content

Decorated functions cannot be overwritten #11

@nostalgiaz

Description

@nostalgiaz

This is what i mean

describe('when used to bind to this context', function () {
    class thisContext {
        protected test: string;

        public constructor() { }

        public getTest(): string {
            return this.test;
        }

        @bind
        public setTest(test: string): void {
            this.test = test;
        }
    }

    it('binds decorated method to this context', function () {
        const tested: thisContext = new thisContext();
        const { setTest } = tested;
        setTest('unit');

        expect(tested.getTest()).toBe('unit');
    });

    it('can be overwritten as well', function () {
        class thisInheritedContext extends thisContext {
            @bind
            public setTest(test: string): void {
                this.test = 'inherited ' + test;
            }
        }

        const tested: thisContext = new thisContext();
        tested.setTest('unit');
        expect(tested.getTest()).toBe('unit');

        const inheritedTested: thisInheritedContext = new thisInheritedContext();
        inheritedTested.setTest('unit');
        expect(inheritedTested.getTest()).toBe('inherited unit');
    });
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions