<commit_before>'use strict';
var Tree = require('../src/js/tree'),
    utils = require('../src/js/util'),
    messages = require('../src/js/consts/messages'),
    Checkbox = require('../src/js/features/checkbox');

jasmine.getFixtures().fixturesPath = 'base/test/fixtures';
describe('Tree', function() {
    var data = [
            {title: 'A', children: [
                {title: '1'},
                {title: '2'},
                {title: '3'},
                {title: '4'},
                {title: '5', children: [
                    {title: '가', children: [
                        {title: '*'}
                    ]},
                    {title: '나'}
                ]},
                {title: '6'},
                {title: '7'},
                {title: '8'},
                {title: '9', children: [
                    {title: '가'},
                    {title: '나'}
                ]},
                {title: '10'},
                {title: '11'},
                {title: '12'}
            ]},
            {title: 'B', children: [
                {title: '1'},
                {title: '2'},
                {title: '3'}
            ]}
        ],
        rootElement, tree;

    beforeEach(function() {
        loadFixtures('basicFixture.html');
        tree = new Tree(data, {
            rootElement: 'treeRoot',
            template: {
                internalNode:
                    '<button type="button" class="{{toggleBtnClass}}">{{stateLabel}}</button>' +
                    '<input type="checkbox" class="tui-tree-checkbox">' +
                    '<span class="{{textClass}}">{{text}}</span>' +
                    '<ul class="{{subtreeClass}}">{{children}}</ul>',
                leafNode:
                    '<input type="checkbox" class="tui-tree-checkbox">' +
                    '<span class="{{textClass}}">{{text}}</span>'
            }
        });

        rootElement = document.getElementById('treeRoot');
        tree.enableFeature('Checkbox', {
            checkboxClassName: 'tui-tree-checkbox'
        });
    });

    it('should have implemented apis about checkbox if enabled', function() {
        var apiList = Checkbox.getAPIList();

        tui.util.forEach(apiList, function(name) {
            expect(tree[name]).not.toThrowError(messages.INVALID_API_CHECKBOX);
        });

        tree.disableFeature('Checkbox');
        tui.util.forEach(apiList, function(name) {
            expect(tree[name]).toThrowError(messages.INVALID_API_CHECKBOX);
        });
    });

    it('should fire "check"-event when a node is checked', function() {
        var firstChildId = tree.getChildIds(tree.getRootNodeId())[0],
            spy = jasmine.createSpy();

        tree.on('check', spy);
        tree.check(firstChildId);

        expect(spy).toHaveBeenCalledWith(firstChildId);
    });

    it('should fire "check"-event when a node is checked', function() {
        var firstChildId = tree.getChildIds(tree.getRootNodeId())[0],
            spy = jasmine.createSpy();

        tree.on('uncheck', spy);
        tree.check(firstChildId);
        tree.uncheck(firstChildId);

        expect(spy).toHaveBeenCalledWith(firstChildId);
    });
});<commit_msg>test: checkbox<commit_after>'use strict';
var Tree = require('../src/js/tree'),
    utils = require('../src/js/util'),
    messages = require('../src/js/consts/messages'),
    Checkbox = require('../src/js/features/checkbox');

jasmine.getFixtures().fixturesPath = 'base/test/fixtures';
describe('Tree', function() {
    var data = [
            {title: 'A', children: [
                {title: '1'},
                {title: '2'},
                {title: '3'},
                {title: '4'},
                {title: '5', children: [
                    {title: '가', children: [
                        {title: '*'}
                    ]},
                    {title: '나'}
                ]},
                {title: '6'},
                {title: '7'},
                {title: '8'},
                {title: '9', children: [
                    {title: '가'},
                    {title: '나'}
                ]},
                {title: '10'},
                {title: '11'},
                {title: '12'}
            ]},
            {title: 'B', children: [
                {title: '1'},
                {title: '2'},
                {title: '3'}
            ]}
        ],
        rootElement, tree;

    beforeEach(function() {
        loadFixtures('basicFixture.html');
        tree = new Tree(data, {
            rootElement: 'treeRoot',
            template: {
                internalNode:
                    '<button type="button" class="{{toggleBtnClass}}">{{stateLabel}}</button>' +
                    '<input type="checkbox" class="tui-tree-checkbox">' +
                    '<span class="{{textClass}}">{{text}}</span>' +
                    '<ul class="{{subtreeClass}}">{{children}}</ul>',
                leafNode:
                    '<input type="checkbox" class="tui-tree-checkbox">' +
                    '<span class="{{textClass}}">{{text}}</span>'
            }
        });

        rootElement = document.getElementById('treeRoot');
        tree.enableFeature('Checkbox', {
            checkboxClassName: 'tui-tree-checkbox'
        });
    });

    it('should have implemented apis about checkbox if enabled', function() {
        var apiList = Checkbox.getAPIList();

        tui.util.forEach(apiList, function(name) {
            expect(tree[name]).not.toThrowError(messages.INVALID_API_CHECKBOX);
        });

        tree.disableFeature('Checkbox');
        tui.util.forEach(apiList, function(name) {
            expect(tree[name]).toThrowError(messages.INVALID_API_CHECKBOX);
        });
    });

    it('should fire "check"-event when a node is checked', function() {
        var firstChildId = tree.getChildIds(tree.getRootNodeId())[0],
            spy = jasmine.createSpy();

        tree.on('check', spy);
        tree.check(firstChildId);

        expect(spy).toHaveBeenCalledWith(firstChildId);
    });

    it('should fire "uncheck"-event when a node is unchecked', function() {
        var firstChildId = tree.getChildIds(tree.getRootNodeId())[0],
            spy = jasmine.createSpy();
        tree.on('uncheck', spy);

        tree.uncheck(firstChildId);
        expect(spy).not.toHaveBeenCalled();

        tree.check(firstChildId);
        tree.uncheck(firstChildId);
        expect(spy).toHaveBeenCalledWith(firstChildId);
    });

    it('should check with all descendants', function() {
        var firstChildId = tree.getChildIds(tree.getRootNodeId())[0];

        tree.check(firstChildId);
        tree.each(function(node, nodeId) {
            expect(tree.isChecked(nodeId)).toBe(true);
        }, firstChildId);
    });

    it('should uncheck with all descendants', function() {
        var firstChildId = tree.getChildIds(tree.getRootNodeId())[0];

        tree.check(firstChildId);
        tree.uncheck(firstChildId);
        tree.each(function(node, nodeId) {
            expect(tree.isUnchecked(nodeId)).toBe(true);
        }, firstChildId);
    });

    it('should toggle with all descendants', function() {
        var firstChildId = tree.getChildIds(tree.getRootNodeId())[0];

        tree.check(firstChildId);
        tree.toggleCheck(firstChildId);
        expect(tree.isUnchecked(firstChildId)).toBe(true);
        tree.each(function(node, nodeId) {
            expect(tree.isUnchecked(nodeId)).toBe(true);
        }, firstChildId);
    });

    it('should be indeterminate node if not all but some children are checked', function() {
        var baseNodeId = tree.getChildIds(tree.getRootNodeId())[0],
            childIds = tree.getChildIds(baseNodeId);

        tree.check(childIds[0]);
        expect(tree.isIndeterminate(baseNodeId)).toBe(true);

        tree.each(function(node, nodeId) {
            tree.check(nodeId);
        }, baseNodeId);
        expect(tree.isIndeterminate(baseNodeId)).toBe(false);
        expect(tree.isChecked(baseNodeId)).toBe(true);

        tree.uncheck(childIds[0]);
        expect(tree.isIndeterminate(baseNodeId)).toBe(true);
    });

    it('should reflect states when a node is changed with children', function() {
        var rootChildIds = tree.getChildIds(tree.getRootNodeId());

        /**
         * ======== Set node states ========
         * a-1(v)
         *   a-1-1(v)
         *   a-1-2(v)
         *   a-1-3(v)
         *   ....
         * a-2(-)
         *   a-2-1(v)
         *   a-2-2( )
         *   a-2-3(v)
         */
        tree.check(rootChildIds[0]);
        tree.check(rootChildIds[1]);
        tree.uncheck(tree.getChildIds(rootChildIds[1])[1]);

        expect(tree.isChecked(rootChildIds[0])).toBe(true);
        expect(tree.isIndeterminate(rootChildIds[1])).toBe(true);

        /**
         * ======== Reflect the changes ========
         * a-1(-)
         *   a-1-1(v)
         *   a-1-2(v)
         *   a-1-3(v)
         *   ....
         *   a-2-2( )  <-- moved to "a-1" from "a-2"
         * a-2(v)
         *   a-2-1(v)
         *   a-2-3(v)
         */
        tree.move(tree.getChildIds(rootChildIds[1])[1], rootChildIds[0]);

        expect(tree.isIndeterminate(rootChildIds[0])).toBe(true);
        expect(tree.isChecked(rootChildIds[1])).toBe(true);
    });
});
{'fraction_non_alphanumeric': 0.16704361873990306, 'fraction_numerical': 0.009369951534733441, 'mean_word_length': 1.7885885885885886, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6473193', 'n_tokens_mistral': 2819, 'n_tokens_neox': 2697, 'n_words': 432}