Completed
Push — master ( 9aa0bc...efafc6 )
by Dmitry
12:01
created

Select2::removeChosenOption()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * HiPanel core package
4
 *
5
 * @link      https://hipanel.com/
6
 * @package   hipanel-core
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\tests\_support\Page\Widget\Input;
12
13
/**
14
 * Class Select2.
15
 *
16
 * Represent Select2 input element.
17
 */
18
class Select2 extends TestableInput
19
{
20
    /**
21
     * @return string
22
     */
23
    protected function getSearchSelector(): string
24
    {
25
        return self::AS_BASE . "div[data-title='{$this->title}']>select";
26
    }
27
28
    /**
29
     * @return string
30
     */
31
    protected function getFilterSelector(): string
32
    {
33
        return self::TF_BASE . "select[id*={$this->auxName}]";
34
    }
35
36
    /**
37
     * @param string $value
38
     * @throws \Exception
39
     */
40
    public function setValue(string $value): void
41
    {
42
        if ($value === '') {
43
            $this->removeChosenOption();
44
            return;
45
        }
46
        $this->open();
47
        $this->fillSearchField($value);
48
        $this->chooseOption($value);
49
    }
50
51
    /**
52
     * @param string $value
53
     * @throws \Exception
54
     */
55
    public function setValueLike(string $value): void
56
    {
57
        $this->open();
58
        $this->fillSearchField($value);
59
        $this->chooseOptionLike($value);
60
    }
61
62
    /**
63
     * @return Select2
64
     */
65
    public function open(): Select2
66
    {
67
        $this->tester->click($this->getClickSelector());
68
        $this->seeIsOpened();
69
70
        return $this;
71
    }
72
73
    /**
74
     * @return Select2
75
     */
76
    public function seeIsOpened(): Select2
77
    {
78
        $this->tester->seeElement('.select2-container--open');
79
80
        return $this;
81
    }
82
83
    /**
84
     * @return string
85
     */
86
    protected function getClickSelector(): string
87
    {
88
        return $this->selector . ' + span [role=combobox]';
89
    }
90
91
    /**
92
     * @param string $name
93
     * @throws \Exception
94
     * @return Select2
95
     */
96
    public function fillSearchField(string $name): Select2
97
    {
98
        $inputSelector = '.select2-container--open input.select2-search__field';
99
        $this->tester->fillField($inputSelector, $name);
100
        $this->tester->waitForElementNotVisible('.loading-results', 120);
101
102
        return $this;
103
    }
104
105
    /**
106
     * @param $optionName
107
     * @return Select2
108
     */
109
    public function chooseOption($optionName): Select2
110
    {
111
        $this->tester->executeJS(<<<JS
112
        $(".select2-results li:contains('{$optionName}')").each(function() {
113
            if (this.firstChild.data === '{$optionName}') {
114
                $(this).trigger('mouseup');
115
            }
116
        });
117
JS
118
        );
119
120
        return $this;
121
    }
122
123
    /**
124
     * @param $optionName
125
     * @return $this
126
     */
127
    public function chooseOptionLike($optionName): Select2
128
    {
129
        $this->tester->executeJS(<<<JS
130
        $(".select2-results li:contains('{$optionName}')").each(function() {
131
            if (this.firstChild.data.indexOf('{$optionName}') !== -1) {
132
                $(this).trigger('mouseup');
133
            }
134
        });
135
JS
136
        );
137
138
        return $this;
139
    }
140
141
    /**
142
     * @return Select2
143
     */
144
    public function removeChosenOption(): Select2
145
    {
146
        $this->tester->executeJS(<<<JS
147
document.querySelector('{$this->getSelector()}').innerHTML += '<option value="" selected></option>'
148
JS
149
        );
150
151
        return $this;
152
    }
153
}
154