# All files / src/user-event/type type.ts

100% Statements 39/39
100% Branches 23/23
100% Functions 3/3
100% Lines 39/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134

    14x
    1x

    13x

    13x
    12x
    12x

    13x
    13x

    13x
    12x

    13x
    13x
    32x
    32x

    32x
    32x

    13x

    13x
    1x
    1x

    13x
    13x

    13x

    32x

    32x

    32x
    2x

    32x

    32x

    32x
    32x

    32x
    2x
    2x

    32x
    2x

    30x
    1x

    29x

     import { ReactTestInstance } from 'react-test-renderer';
    import { getConfig } from '../../config';
    import { EventBuilder } from '../event-builder';
    import { ErrorWithStack } from '../../helpers/errors';
    import { UserEventInstance } from '../setup';
    import {
      dispatchOwnHostEvent,
      wait,
      getTextRange,
      getTextContentSize,
    } from '../utils';

    import { parseKeys } from './parseKeys';

    export interface TypeOptions {
      skipPress?: boolean;
      submitEditing?: boolean;
    }

    export async function type(
      this: UserEventInstance,
      element: ReactTestInstance,
      text: string,
      options?: TypeOptions
    ): Promise<void> {
      if (element.type !== getConfig().hostComponentNames?.textInput) {
        throw new ErrorWithStack(
          `type() works only with "TextInput" elements. Passed element has type "${element.type}".`,
          type
        );
      }

      const keys = parseKeys(text);

      if (options?.skipPress !== true) {
        await wait(this.config);
        dispatchOwnHostEvent(element, 'pressIn', EventBuilder.Common.touch());
      }

      await wait(this.config);
      dispatchOwnHostEvent(element, 'focus', EventBuilder.Common.focus());

      if (options?.skipPress !== true) {
        dispatchOwnHostEvent(element, 'pressOut', EventBuilder.Common.touch());
      }

      let currentText = element.props.value ?? element.props.defaultValue ?? '';
      for (const key of keys) {
        const previousText = element.props.value ?? currentText;
        currentText = applyKey(previousText, key);

        await wait(this.config);
        emitTypingEvents(element, key, currentText, previousText);
      }

      const finalText = element.props.value ?? currentText;

      if (options?.submitEditing === true) {
        await wait(this.config);
        dispatchOwnHostEvent(
          element,
          'submitEditing',
          EventBuilder.TextInput.submitEditing(finalText)
        );
      }

      await wait(this.config);
      dispatchOwnHostEvent(
        element,
        'endEditing',
        EventBuilder.TextInput.endEditing(finalText)
      );
      dispatchOwnHostEvent(element, 'blur', EventBuilder.Common.blur());
    }

    async function emitTypingEvents(
      element: ReactTestInstance,
      key: string,
      currentText: string,
      previousText: string
    ) {
      const isMultiline = element.props.multiline === true;

      dispatchOwnHostEvent(
        element,
        'keyPress',
        EventBuilder.TextInput.keyPress(key)
      );

      if (isMultiline) {
        dispatchOwnHostEvent(
          element,
          'textInput',
          EventBuilder.TextInput.textInput(currentText, previousText)
        );
      }

      dispatchOwnHostEvent(
        element,
        'change',
        EventBuilder.TextInput.change(currentText)
      );

      dispatchOwnHostEvent(element, 'changeText', currentText);

      const selectionRange = getTextRange(currentText);
      dispatchOwnHostEvent(
        element,
        'selectionChange',
        EventBuilder.TextInput.selectionChange(selectionRange)
      );

      if (isMultiline) {
        const contentSize = getTextContentSize(currentText);
        dispatchOwnHostEvent(
          element,
          'contentSizeChange',
          EventBuilder.TextInput.contentSizeChange(contentSize)
        );
      }
    }

    function applyKey(text: string, key: string) {
      if (key === 'Enter') {
        return `${text}\n`;
      }

      if (key === 'Backspace') {
        return text.slice(0, -1);
      }

      return text + key;
    }