Best JavaScript code snippet using jest-extended
MCTTableControllerSpec.js
Source:MCTTableControllerSpec.js  
1/*****************************************************************************2 * Open MCT, Copyright (c) 2014-2016, United States Government3 * as represented by the Administrator of the National Aeronautics and Space4 * Administration. All rights reserved.5 *6 * Open MCT is licensed under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance with the License.8 * You may obtain a copy of the License at9 * http://www.apache.org/licenses/LICENSE-2.0.10 *11 * Unless required by applicable law or agreed to in writing, software12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the14 * License for the specific language governing permissions and limitations15 * under the License.16 *17 * Open MCT includes source code licensed under additional open source18 * licenses. See the Open Source Licenses file (LICENSES.md) included with19 * this source code distribution or the Licensing information page available20 * at runtime from the About dialog for additional information.21 *****************************************************************************/22define(23    [24        "zepto",25        "../../src/controllers/MCTTableController"26    ],27    function ($, MCTTableController) {28        var MOCK_ELEMENT_TEMPLATE =29            '<div><div class="l-view-section scrolling">' +30                '<table class="sizing-table"><tbody></tbody></table>' +31                '<table class="mct-table"><thead></thead></table>' +32            '</div></div>';33        describe('The MCTTable Controller', function () {34            var controller,35                mockScope,36                watches,37                mockTimeout,38                mockElement,39                mockExportService;40            function promise(value) {41                return {42                    then: function (callback) {43                        return promise(callback(value));44                    }45                };46            }47            beforeEach(function () {48                watches = {};49                mockScope = jasmine.createSpyObj('scope', [50                   '$watch',51                   '$on',52                   '$watchCollection'53                ]);54                mockScope.$watchCollection.andCallFake(function (event, callback) {55                    watches[event] = callback;56                });57                mockElement = $(MOCK_ELEMENT_TEMPLATE);58                mockExportService = jasmine.createSpyObj('exportService', [59                    'exportCSV'60                ]);61                mockScope.displayHeaders = true;62                mockTimeout = jasmine.createSpy('$timeout');63                mockTimeout.andReturn(promise(undefined));64                controller = new MCTTableController(65                    mockScope,66                    mockTimeout,67                    mockElement,68                    mockExportService69                );70                spyOn(controller, 'setVisibleRows').andCallThrough();71            });72            it('Reacts to changes to filters, headers, and rows', function () {73                expect(mockScope.$watchCollection).toHaveBeenCalledWith('filters', jasmine.any(Function));74                expect(mockScope.$watch).toHaveBeenCalledWith('headers', jasmine.any(Function));75                expect(mockScope.$watch).toHaveBeenCalledWith('rows', jasmine.any(Function));76            });77            describe('rows', function () {78                var testRows = [];79                beforeEach(function () {80                    testRows = [81                        {82                            'col1': {'text': 'row1 col1 match'},83                            'col2': {'text': 'def'},84                            'col3': {'text': 'row1 col3'}85                        },86                        {87                            'col1': {'text': 'row2 col1 match'},88                            'col2': {'text': 'abc'},89                            'col3': {'text': 'row2 col3'}90                        },91                        {92                            'col1': {'text': 'row3 col1'},93                            'col2': {'text': 'ghi'},94                            'col3': {'text': 'row3 col3'}95                        }96                    ];97                    mockScope.rows = testRows;98                });99                it('Filters results based on filter input', function () {100                    var filters = {},101                        filteredRows;102                    mockScope.filters = filters;103                    filteredRows = controller.filterRows(testRows);104                    expect(filteredRows.length).toBe(3);105                    filters.col1 = 'row1';106                    filteredRows = controller.filterRows(testRows);107                    expect(filteredRows.length).toBe(1);108                    filters.col1 = 'match';109                    filteredRows = controller.filterRows(testRows);110                    expect(filteredRows.length).toBe(2);111                });112                it('Sets rows on scope when rows change', function () {113                    controller.setRows(testRows);114                    expect(mockScope.displayRows.length).toBe(3);115                    expect(mockScope.displayRows).toEqual(testRows);116                });117                it('Supports adding rows individually', function () {118                    var addRowFunc = mockScope.$on.calls[mockScope.$on.calls.length - 2].args[1],119                        row4 = {120                            'col1': {'text': 'row3 col1'},121                            'col2': {'text': 'ghi'},122                            'col3': {'text': 'row3 col3'}123                        };124                    controller.setRows(testRows);125                    expect(mockScope.displayRows.length).toBe(3);126                    testRows.push(row4);127                    addRowFunc(undefined, 3);128                    expect(mockScope.displayRows.length).toBe(4);129                });130                it('Supports removing rows individually', function () {131                    var removeRowFunc = mockScope.$on.calls[mockScope.$on.calls.length - 1].args[1];132                    controller.setRows(testRows);133                    expect(mockScope.displayRows.length).toBe(3);134                    removeRowFunc(undefined, 2);135                    expect(mockScope.displayRows.length).toBe(2);136                    expect(controller.setVisibleRows).toHaveBeenCalled();137                });138                it("can be exported as CSV", function () {139                    controller.setRows(testRows);140                    controller.setHeaders(Object.keys(testRows[0]));141                    mockScope.exportAsCSV();142                    expect(mockExportService.exportCSV)143                        .toHaveBeenCalled();144                    mockExportService.exportCSV.mostRecentCall.args[0]145                        .forEach(function (row, i) {146                            Object.keys(row).forEach(function (k) {147                                expect(row[k]).toEqual(148                                    mockScope.displayRows[i][k].text149                                );150                            });151                        });152                });153                describe('sorting', function () {154                    var sortedRows;155                    it('Sorts rows ascending', function () {156                        mockScope.sortColumn = 'col1';157                        mockScope.sortDirection = 'asc';158                        sortedRows = controller.sortRows(testRows);159                        expect(sortedRows[0].col1.text).toEqual('row1 col1 match');160                        expect(sortedRows[1].col1.text).toEqual('row2 col1' +161                            ' match');162                        expect(sortedRows[2].col1.text).toEqual('row3 col1');163                    });164                    it('Sorts rows descending', function () {165                        mockScope.sortColumn = 'col1';166                        mockScope.sortDirection = 'desc';167                        sortedRows = controller.sortRows(testRows);168                        expect(sortedRows[0].col1.text).toEqual('row3 col1');169                        expect(sortedRows[1].col1.text).toEqual('row2 col1 match');170                        expect(sortedRows[2].col1.text).toEqual('row1 col1 match');171                    });172                    it('Sorts rows descending based on selected sort column', function () {173                        mockScope.sortColumn = 'col2';174                        mockScope.sortDirection = 'desc';175                        sortedRows = controller.sortRows(testRows);176                        expect(sortedRows[0].col2.text).toEqual('ghi');177                        expect(sortedRows[1].col2.text).toEqual('def');178                        expect(sortedRows[2].col2.text).toEqual('abc');179                    });180                    // https://github.com/nasa/openmct/issues/910181                    it('updates visible rows in scope', function () {182                        var oldRows;183                        mockScope.rows = testRows;184                        controller.setRows(testRows);185                        oldRows = mockScope.visibleRows;186                        mockScope.toggleSort('col2');187                        expect(mockScope.visibleRows).not.toEqual(oldRows);188                    });189                    it('correctly sorts rows of differing types', function () {190                        mockScope.sortColumn = 'col2';191                        mockScope.sortDirection = 'desc';192                        testRows.push({193                            'col1': {'text': 'row4 col1'},194                            'col2': {'text': '123'},195                            'col3': {'text': 'row4 col3'}196                        });197                        testRows.push({198                            'col1': {'text': 'row5 col1'},199                            'col2': {'text': '456'},200                            'col3': {'text': 'row5 col3'}201                        });202                        testRows.push({203                            'col1': {'text': 'row5 col1'},204                            'col2': {'text': ''},205                            'col3': {'text': 'row5 col3'}206                        });207                        sortedRows = controller.sortRows(testRows);208                        expect(sortedRows[0].col2.text).toEqual('ghi');209                        expect(sortedRows[1].col2.text).toEqual('def');210                        expect(sortedRows[2].col2.text).toEqual('abc');211                        expect(sortedRows[sortedRows.length - 3].col2.text).toEqual('456');212                        expect(sortedRows[sortedRows.length - 2].col2.text).toEqual('123');213                        expect(sortedRows[sortedRows.length - 1].col2.text).toEqual('');214                    });215                    describe('The sort comparator', function () {216                        it('Correctly sorts different data types', function () {217                            var val1 = "",218                                val2 = "1",219                                val3 = "2016-04-05 18:41:30.713Z",220                                val4 = "1.1",221                                val5 = "8.945520958175627e-13";222                            mockScope.sortDirection = "asc";223                            expect(controller.sortComparator(val1, val2)).toEqual(-1);224                            expect(controller.sortComparator(val3, val1)).toEqual(1);225                            expect(controller.sortComparator(val3, val2)).toEqual(1);226                            expect(controller.sortComparator(val4, val2)).toEqual(1);227                            expect(controller.sortComparator(val2, val5)).toEqual(1);228                        });229                    });230                    describe('Adding new rows', function () {231                        var row4,232                            row5,233                            row6;234                        beforeEach(function () {235                            row4 = {236                                'col1': {'text': 'row5 col1'},237                                'col2': {'text': 'xyz'},238                                'col3': {'text': 'row5 col3'}239                            };240                            row5 = {241                                'col1': {'text': 'row6 col1'},242                                'col2': {'text': 'aaa'},243                                'col3': {'text': 'row6 col3'}244                            };245                            row6 = {246                                'col1': {'text': 'row6 col1'},247                                'col2': {'text': 'ggg'},248                                'col3': {'text': 'row6 col3'}249                            };250                        });251                        it('Adds new rows at the correct sort position when' +252                            ' sorted ', function () {253                            mockScope.sortColumn = 'col2';254                            mockScope.sortDirection = 'desc';255                            mockScope.displayRows = controller.sortRows(testRows.slice(0));256                            mockScope.rows.push(row4);257                            controller.addRow(undefined, mockScope.rows.length - 1);258                            expect(mockScope.displayRows[0].col2.text).toEqual('xyz');259                            mockScope.rows.push(row5);260                            controller.addRow(undefined, mockScope.rows.length - 1);261                            expect(mockScope.displayRows[4].col2.text).toEqual('aaa');262                            mockScope.rows.push(row6);263                            controller.addRow(undefined, mockScope.rows.length - 1);264                            expect(mockScope.displayRows[2].col2.text).toEqual('ggg');265                            //Add a duplicate row266                            mockScope.rows.push(row6);267                            controller.addRow(undefined, mockScope.rows.length - 1);268                            expect(mockScope.displayRows[2].col2.text).toEqual('ggg');269                            expect(mockScope.displayRows[3].col2.text).toEqual('ggg');270                        });271                        it('Adds new rows at the correct sort position when' +272                            ' sorted and filtered', function () {273                            mockScope.sortColumn = 'col2';274                            mockScope.sortDirection = 'desc';275                            mockScope.filters = {'col2': 'a'};//Include only276                            // rows with 'a'277                            mockScope.displayRows = controller.sortRows(testRows.slice(0));278                            mockScope.displayRows = controller.filterRows(testRows);279                            mockScope.rows.push(row5);280                            controller.addRow(undefined, mockScope.rows.length - 1);281                            expect(mockScope.displayRows.length).toBe(2);282                            expect(mockScope.displayRows[1].col2.text).toEqual('aaa');283                            mockScope.rows.push(row6);284                            controller.addRow(undefined, mockScope.rows.length - 1);285                            expect(mockScope.displayRows.length).toBe(2);286                            //Row was not added because does not match filter287                        });288                        it('Adds new rows at the correct sort position when' +289                            ' not sorted ', function () {290                            mockScope.sortColumn = undefined;291                            mockScope.sortDirection = undefined;292                            mockScope.filters = {};293                            mockScope.displayRows = testRows.slice(0);294                            mockScope.rows.push(row5);295                            controller.addRow(undefined, mockScope.rows.length - 1);296                            expect(mockScope.displayRows[3].col2.text).toEqual('aaa');297                            mockScope.rows.push(row6);298                            controller.addRow(undefined, mockScope.rows.length - 1);299                            expect(mockScope.displayRows[4].col2.text).toEqual('ggg');300                        });301                        it('Resizes columns if length of any columns in new' +302                            ' row exceeds corresponding existing column', function () {303                            var row7 = {304                                'col1': {'text': 'row6 col1'},305                                'col2': {'text': 'some longer string'},306                                'col3': {'text': 'row6 col3'}307                            };308                            mockScope.sortColumn = undefined;309                            mockScope.sortDirection = undefined;310                            mockScope.filters = {};311                            mockScope.displayRows = testRows.slice(0);312                            mockScope.rows.push(row7);313                            controller.addRow(undefined, mockScope.rows.length - 1);314                            expect(controller.$scope.sizingRow.col2).toEqual({text: 'some longer string'});315                        });316                    });317                });318            });319        });...Using AI Code Generation
1const { testRows } = require('jest-extended');2test('testRows', () => {3  expect([4  ]).toTestRows([5  ]);6});7  ✓ testRows (3ms)8const { testRows } = require('jest-extended');9test('testRows', () => {10  expect([11  ]).toTestRows([12  ]);13});14  ✕ testRows (3ms)15    expect(received).toTestRows(expected)16const { testRows } = require('jest-extended');17test('testRows', () => {18  expect([Using AI Code Generation
1const { testRows } = require('jest-extended');2test('testRows', () => {3    ];4    expect(data).toIncludeAllRows([[1, 2, 3], [4, 5, 6]]);5    expect(data).toIncludeAllRows([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);6    expect(data).not.toIncludeAllRows([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]);7    expect(data).not.toIncludeAllRows([[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]);8    expect(data).not.toIncludeAllRows([[1, 2, 3], [4, 5, 6], [7, 8, 9, 10], [10, 11, 12]]);9    expect(data).not.toIncludeAllRows([[1, 2, 3], [4, 5, 6], [7, 8, 10]]);10    expect(data).not.toIncludeAllRows([[1, 2, 3], [4, 5, 6], [7, 8, 10], [10, 11, 12]]);11});12const { testColumns } = require('jest-extended');13test('testColumns', () => {14    ];15    expect(data).toIncludeAllColumns([[1, 4, 7], [2, 5, 8], [3, 6, 9]]);16    expect(data).toIncludeAllColumns([[1, 4, 7], [2, 5, 8], [3, 6, 9], [10, 11, 12]]);17    expect(data).not.toIncludeAllColumns([[1,Using AI Code Generation
1const { testRows } = require('jest-extended');2const { testRows } = require('jest-extended/dist/matchers/toHaveRows');3const { testRows } = require('jest-extended/dist/matchers/toHaveRows/index');4const { testRows } = require('jest-extended');5const { testRows } = require('jest-extended/dist/matchers/toHaveRows');6const { testRows } = require('jest-extended/dist/matchers/toHaveRows/index');7const { testRows } = require('jest-extended');8const { testRows } = require('jest-extended/dist/matchers/toHaveRows');9const { testRows } = require('jest-extended/dist/matchers/toHaveRows/index');10const { testRows } = require('jest-extended');11const { testRows } = require('jest-extended/dist/matchers/toHaveRows');12const { testRows } = require('jest-extended/dist/matchers/toHaveRows/index');13const { testRows } = require('jest-extended');14const { testRows } = require('jest-extended/dist/matchers/toHaveRows');15const { testRows } = require('jest-extended/dist/matchers/toHaveRows/index');16const { testRows } = require('jest-extended');17const { testRows } = require('jest-extended/dist/matchers/toHaveRows');18const { testRows } = require('jest-extended/dist/matchers/toHaveRows/index');19const { testRows } = require('jest-extended');20const { testRows } = require('jest-extended/dist/matchers/toHaveRows');21const { testRows } = require('jest-extended/dist/matchers/toHaveRows/index');22const { testRows } = require('jest-extended');23const { testRowsUsing AI Code Generation
1expect(testRows).toBeArrayOfSize(2);2expect(testRows).toContainAllValues(['row1', 'row2']);3expect(testRows).not.toContainAnyValues(['row3', 'row4']);4expect(testRows).toBeArrayOfSize(2);5expect(testRows).toContainAllValues(['row1', 'row2']);6expect(testRows).not.toContainAnyValues(['row3', 'row4']);7expect(testRows).toBeArrayOfSize(2);8expect(testRows).toContainAllValues(['row1', 'row2']);9expect(testRows).not.toContainAnyValues(['row3', 'row4']);10expect(testRows).toBeArrayOfSize(2);11expect(testRows).toContainAllValues(['row1', 'row2']);12expect(testRows).not.toContainAnyValues(['row3', 'row4']);13expect(testRows).toBeArrayOfSize(2);14expect(testRows).toContainAllValues(['row1', 'row2']);15expect(testRows).not.toContainAnyValues(['row3', 'row4']);16expect(testRows).toBeArrayOfSize(2);17expect(testRows).toContainAllValues(['row1', 'row2']);18expect(testRows).not.toContainAnyValues(['row3', 'row4']);19expect(testRows).toBeArrayOfSize(2);20expect(testRows).toContainAllValues(['row1', 'row2']);21expect(testRows).not.toContainAnyValues(['row3', 'row4']);22expect(testRows).toBeArrayOfSize(2);23expect(testRows).toContainAllValues(['row1', 'row2']);24expect(testRows).not.toContainAnyValues(['row3', 'row4']);25expect(testRows).toBeArrayOfSize(2);26expect(testRows).toContainAllValues(['rowUsing AI Code Generation
1const { testRows } = require('jest-extended');2const { test } = require('jest-extended');3const { expect } = require('jest-extended');4test('testRows', () => {5    { name: 'John Doe', age: 20 },6    { name: 'Jane Doe', age: 21 },7  ];8  testRows(rows, 'name', 'age');9});10const { testRows } = require('jest-extended');11const { test } = require('jest-extended');12const { expect } = require('jest-extended');13test('testRows', () => {14    { name: 'John Doe', age: 20 },15    { name: 'Jane Doe', age: 21 },16  ];17  testRows(rows, 'name', 'age');18});Using AI Code Generation
1const { testRows } = require('jest-extended');2const db = require('./db');3const { connection } = db;4describe('db', () => {5  beforeAll(async () => {6    await connection.connect();7  });8  afterAll(async () => {9    await connection.close();10  });11  test('should return 2 rows', async () => {12    const rows = await db.getUsers();13    testRows(rows, 2);14  });15});Using AI Code Generation
1const { testRows } = require('jest-extended');2const { getRows } = require('jest-extended');3const { getColumns } = require('jest-extended');4const { testColumns } = require('jest-extended');5const { testDiagonals } = require('jest-extended');6const { testDiagonals } = require('jest-extended');7describe('testRows', () => {8  it('should return true for a winning row', () => {9    ];10    expect(testRows(board)).toBeTruthy();11  });12  it('should return false for a non winning row', () => {13    ];14    expect(testRows(board)).toBeFalsy();15  });16});17describe('testColumns', () => {18  it('should return true for a winning column', () => {19    ];20    expect(testColumns(board)).toBeTruthy();21  });22  it('should return false for a non winning column', () => {23    ];24    expect(testColumns(board)).toBeFalsy();25  });26});27describe('testDiagonals', () => {28  it('should return true for a winning diagonal', () => {29    ];30    expect(testDiagonals(board)).toBeTruthy();31  });32  it('should return false for a non winning diagonal', () => {Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
