Skip to content

Commit a5e8da9

Browse files
AgentofGamingbenjamincharity
authored andcommitted
Copy config number array to prevent it being modified in keypad constructor (#1) (#10)
* Config numbers invariance should be enforced. When we instantiate bc-keypad the array owned by config should not be changed * Check on multiple instantiations
1 parent e7770db commit a5e8da9

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

src/keypad.controller.spec.js

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ describe('KeypadController', () => {
207207

208208
it('should add to the number model when number key is clicked', () => {
209209
const ORIGINAL_LENGTH = vm.bcNumberModel.length;
210-
const numberButton = element[0].querySelectorAll('.bc-keypad__key > button')[2];
210+
const buttonArray = element[0].querySelectorAll('.bc-keypad__key > button');
211+
const numberButton = buttonArray[2];
211212
angular.element(numberButton).triggerHandler('click');
212213

213214
expect(vm.bcNumberModel.length).toEqual(ORIGINAL_LENGTH + 1);
@@ -277,6 +278,66 @@ describe('KeypadController', () => {
277278

278279
});
279280

281+
describe('Config Invariance', () => {
282+
let $scope;
283+
let element;
284+
let vm;
285+
const defaultNumbers =
286+
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]; // eslint-disable-line no-magic-numbers
287+
288+
beforeEach(() => {
289+
$scope = $rootScope.$new();
290+
$scope.buttonLeft = function($event, numbers) {};
291+
$scope.buttonRight = function($event, numbers) {};
292+
$scope.numbers = '';
293+
element = angular.element(`
294+
<bc-keypad
295+
bc-number-model="numbers"
296+
bc-left-button-method="buttonLeft($event, numbers)"
297+
bc-right-button-method="buttonRight($event, numbers)"
298+
></bc-keypad>
299+
`);
300+
element = $compile(element)($scope);
301+
$scope.$apply();
302+
vm = element.isolateScope().vm;
303+
304+
spyOn($scope, 'buttonLeft');
305+
spyOn($scope, 'buttonRight');
306+
});
307+
308+
it('config should still contain the same numbers after keypad constructed', () => {
309+
const buttons = vm.bcKeypadConfig.numbers;
310+
311+
expect(buttons).toEqual(defaultNumbers);
312+
});
313+
314+
it('config should still remain the same after keypad constructions', () => {
315+
for (let i = 0; i < 3; i = i + 1) { // eslint-disable-line no-magic-numbers
316+
$scope = $rootScope.$new();
317+
$scope.numbers = '';
318+
element = angular.element(`
319+
<bc-keypad
320+
bc-number-model="numbers"
321+
></bc-keypad>
322+
`);
323+
element = $compile(element)($scope);
324+
$scope.$apply();
325+
vm = element.isolateScope().vm;
326+
327+
const buttons = vm.bcKeypadConfig.numbers;
328+
329+
expect(buttons).toEqual(defaultNumbers);
330+
}
331+
});
332+
333+
it('the keypad should have 12 buttons given a left and right method', () => {
334+
const buttonArray = element[0].querySelectorAll('.bc-keypad__key');
335+
336+
expect(buttonArray.length).toEqual(defaultNumbers.length + 2);
337+
});
338+
339+
});
340+
280341

281342
});
282343

0 commit comments

Comments
 (0)