Q:

how to define keyCode or which or key or code properties for keyboard event

As per my requirement, we need to write test specs for hot keys (say shift+1+L -- upload file).In my method i have written something like below 
    public static SHIFTKEY      : number = 16;
    this.keys = {};

    document.addEventListener('keydown', (event: KeyboardEvent) => {
    var code = event.which ? event.which : event.keyCode;
    if (code === KeyCodeConst.SHIFTKEY || (this.keys[KeyCodeConst.SHIFTKEY] && trackedKeys[code])) {
    if (!this.keys[code]) {
    this.keys[code] = true;
    this.keysCount++;
    }
    if (this.keysCount === 3) {
        alert('call the method which is required');
    }
    }
    if (!trackedKeys[code]) {
    this.keysCount = 0;
    }
    });

Now in my spec file i am trying to write a test case for this like below 
      describe('Add Event Listener', () => {
           beforeEach(() => {
               viewModel = new viewModel();
               spyOn(window, 'addEventListener').and.callThrough();
           });
           it('check the combination of keys are called', () => {
               keyPress(16);
               keyPress(49);
               keyPress(76);
               expect(viewModel.keys[KeyCodeConst.SHIFTKEY]).toBeTruthy();          
           });
           function keyPress(keyCodeVal) {
               var keyboardEvent = document.createEvent('KeyboardEvent');
               try {
                   Object.defineProperty(keyboardEvent, 'keyCode', {'value': keyCodeVal});
               } catch (err){
                   console.log(err);
               }
               keyboardEvent.initKeyboardEvent('keydown',true,false,window,'',0,'',false,'');
               document.body.dispatchEvent(keyboardEvent);
           }
       });

While running the build , i am getting an error like below and it is giving an build error like below 
    Attempting to change value of a readonly property.
    at defineProperty (--some url--)
    at keyPress (--some url--)

Is there a way where we can send the pressed key value to the method ,apart from  keyCode or which. As per the MDN guidelines i observed that these are readonly attributes .I tried using key but it didn't work .  

A:

To test your method without actually triggering a keydown event, you might seperate the logic of getting the pressed key out of your function and in your tests just feed it with the pressed key codes you like to test.
To actually test keydown events, try this function which takes the element to dispatch the keydown event to and the keyCode value (adjusted to phantom-js):
    function triggerKeyDown(element, value) {
        var e = document.createEvent('KeyboardEvent');
        e.initEvent('keydown', true, false);
        Object.defineProperty(e, 'keyCode', {'value': value});
        element.dispatchEvent(e);
    }

