can i set caret position in textarea?

172 views
Skip to first unread message

ZhiXuan Guan

unread,
Aug 23, 2013, 3:55:50 AM8/23/13
to jasmi...@googlegroups.com
I am trying to implement '@ Users Feature' with angularjs, I almost finish the feature except writing an unit test. I have a Caret module which can help me get/set the caret position in textarea.

I think the most important thing is to get/set the caret position but I dont know how to do it in jasmine.

Mydirective

    .directive('atUser', function (Caret) {
        return {
            restrict: 'A',
            link: function (scope, element) {
                element.bind('focus click keydown', function () {
                    scope.caretPos = Caret.getPos(element);
                });

                scope.$watch(function () {
                    return scope.caretPos;
                }, function (nowCaretPos) {
                    /* do something here */
                })
            }
        }
    })

Html

    <textarea ng-model="message" at-user></textarea>

Jasmine
    
    describe('test at user', function () {
        /* some init code */
        it('should get caret postion', function () {
            textarea = element.find('textarea');
            textarea.triggerHandler('focus');
            except(textarea.scope().caretPos).toEqual(0);

            /*
             * then i want to simulate keydown event and type something
             * and get the caret postion
             * but i dont know how to do it
             * /
        })
    })

Can anyone help me?

thanks a lot!
Reply all
Reply to author
Forward
0 new messages