<commit_before>
import {EasyWatch as Watch} from '../src/index';

describe('dirty checker', function() {

    it('one level object', function() {
        var obj = {name: 'Beijing'};
        var watcher = new Watch(obj);

        watcher.subscribe(function() {
            expect(obj.name).to.be.equal('Shanghai');
        });

        obj.name = 'Shanghai';
    });

    it('one level object: multiple subscribers', function() {
        var obj = {name: 'Beijing'};
        var watcher = new Watch(obj);

        watcher.subscribe(function() {
            expect(obj.name).to.be.equal('Shanghai');
        });

        watcher.subscribe(function() {
            expect(obj.name).to.be.equal('Shanghai');
        });

        obj.name = 'Shanghai';
    });

    it('one level object: multiple properties', function(done) {
        var obj = {name: 'Beijing', size: 16410.54, population: 2152};
        var watcher = new Watch(obj);

        var counter = 0;
        watcher.subscribe(function() {
            if (counter === 0) {
                expect(obj.name).to.be.equal('Shanghai');
            } else if (counter === 1) {
                expect(obj.size).to.be.equal(6340);
            } else if (counter === 2) {
                expect(obj.population).to.be.equal(2426);
                done();
            }
            counter++;
        });

        setTimeout(function() {
            obj.name = 'Shanghai';
            obj.size = 6340;
            obj.population = 2426;
        });
    });

    it('two levels object', function(done) {
        var user = {
            name: 'Xiaoguang',
            child: {
                nickname: 'xxguang',
                age: 11
            }
        };
        var watcher = new Watch(user);

        watcher.subscribe(function() {
            expect(user.child.age).to.be.equal(9);
            done();
        });

        setTimeout(function() {
            user.child.age = 9;
        });
    });

    it('two levels object: change child reference', function(done) {
        var user = {
            name: 'Xiaoguang',
            child: {
                nickname: 'xxguang',
                age: 11
            }
        };
        var watcher = new Watch(user);

        watcher.subscribe(function() {
            expect(user.child.noway).to.be.equal('nonono');
            done();
        });

        setTimeout(function() {
            user.child = {noway: 'nonono'};
        });
    });

    it('simeple array', function(done) {
        var arr = ['Beijing', 'Shanghai'];
        var watcher = new Watch(arr);

        watcher.subscribe(function() {
            expect(arr.length).to.be.equal(1);
            expect(arr[0]).to.be.equal('Beijing');
            done();
        });

        setTimeout(function() {
            arr.pop();
        });
    });

    it('array with simple object', function(done) {
        var arr = [{name: 'Beijing'}, {name: 'Shanghai'}];
        var watcher = new Watch(arr);

        watcher.subscribe(function() {
            expect(arr.length).to.be.equal(2);
            expect(arr[0].name).to.be.equal('Nanjing');
            done();
        });

        setTimeout(function() {
            arr[0].name = 'Nanjing';
        });
    });

    it('object with array as property', function(done) {
        var obj = {
            name: 'Beijing',
            districts: ['Daxing', 'Changping', 'Haidian', 'Chaoyang']
        };
        var watcher = new Watch(obj);

        watcher.subscribe(function() {
            expect(obj.districts.length).to.be.equal(6);
            expect(obj.districts[5]).to.be.equal('Fengtai');
            done();
        });

        setTimeout(function() {
            obj.districts.push('Tongzhou', 'Fengtai');
        });
    });
});<commit_msg>add test case for unsubscribe<commit_after>
import {EasyWatch as Watch} from '../src/index';

describe('dirty checker', function() {

    it('one level object', function() {
        var obj = {name: 'Beijing'};
        var watcher = new Watch(obj);

        watcher.subscribe(function() {
            expect(obj.name).to.be.equal('Shanghai');
        });

        obj.name = 'Shanghai';
    });

    it('one level object: multiple subscribers', function() {
        var obj = {name: 'Beijing'};
        var watcher = new Watch(obj);

        watcher.subscribe(function() {
            expect(obj.name).to.be.equal('Shanghai');
        });

        watcher.subscribe(function() {
            expect(obj.name).to.be.equal('Shanghai');
        });

        obj.name = 'Shanghai';
    });

    it('one level object: multiple properties', function(done) {
        var obj = {name: 'Beijing', size: 16410.54, population: 2152};
        var watcher = new Watch(obj);

        var counter = 0;
        watcher.subscribe(function() {
            if (counter === 0) {
                expect(obj.name).to.be.equal('Shanghai');
            } else if (counter === 1) {
                expect(obj.size).to.be.equal(6340);
            } else if (counter === 2) {
                expect(obj.population).to.be.equal(2426);
                done();
            }
            counter++;
        });

        setTimeout(function() {
            obj.name = 'Shanghai';
            obj.size = 6340;
            obj.population = 2426;
        });
    });

    it('two levels object', function(done) {
        var user = {
            name: 'Xiaoguang',
            child: {
                nickname: 'xxguang',
                age: 11
            }
        };
        var watcher = new Watch(user);

        watcher.subscribe(function() {
            expect(user.child.age).to.be.equal(9);
            done();
        });

        setTimeout(function() {
            user.child.age = 9;
        });
    });

    it('two levels object: change child reference', function(done) {
        var user = {
            name: 'Xiaoguang',
            child: {
                nickname: 'xxguang',
                age: 11
            }
        };
        var watcher = new Watch(user);

        watcher.subscribe(function() {
            expect(user.child.noway).to.be.equal('nonono');
            done();
        });

        setTimeout(function() {
            user.child = {noway: 'nonono'};
        });
    });

    it('simeple array', function(done) {
        var arr = ['Beijing', 'Shanghai'];
        var watcher = new Watch(arr);

        watcher.subscribe(function() {
            expect(arr.length).to.be.equal(1);
            expect(arr[0]).to.be.equal('Beijing');
            done();
        });

        setTimeout(function() {
            arr.pop();
        });
    });

    it('array with simple object', function(done) {
        var arr = [{name: 'Beijing'}, {name: 'Shanghai'}];
        var watcher = new Watch(arr);

        watcher.subscribe(function() {
            expect(arr.length).to.be.equal(2);
            expect(arr[0].name).to.be.equal('Nanjing');
            done();
        });

        setTimeout(function() {
            arr[0].name = 'Nanjing';
        });
    });

    it('object with array as property', function(done) {
        var obj = {
            name: 'Beijing',
            districts: ['Daxing', 'Changping', 'Haidian', 'Chaoyang']
        };
        var watcher = new Watch(obj);

        watcher.subscribe(function() {
            expect(obj.districts.length).to.be.equal(6);
            expect(obj.districts[5]).to.be.equal('Fengtai');
            done();
        });

        setTimeout(function() {
            obj.districts.push('Tongzhou', 'Fengtai');
        });
    });

    it('unsubscribe', function(done) {
        var obj = {name: 'Beijing'};
        var watcher = new Watch(obj);

        var counter = 0;
        var unsubscribe = watcher.subscribe(() => {
            counter++;
        });

        unsubscribe();

        setTimeout(function() {
            obj.name = 'Shanghai';
            setTimeout(function() {
                expect(counter).to.be.equal(0);
                done();
            });
        });

    });
});
{'fraction_non_alphanumeric': 0.15888432324741625, 'fraction_numerical': 0.011206574523720583, 'mean_word_length': 1.5718860070445084, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '4574223', 'n_tokens_mistral': 2450, 'n_tokens_neox': 2313, 'n_words': 440}