Skip to content

Commit

Permalink
test: increase test coverage, ignore unreachable lines
Browse files Browse the repository at this point in the history
  • Loading branch information
JaredReisinger committed Dec 13, 2022
1 parent b9dd973 commit fda3353
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/CrosswordProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,10 @@ const CrosswordProvider = React.forwardRef<
(row: number, col: number) => {
const cell = getCellData(row, col);
if (!cell.used) {
// Because this is in an internal callback, and we only call it with a
// valid cell (row/col), the throw line isn't testable... so we ignore
// it.
/* istanbul ignore next */
throw new Error('unexpected unused cell');
}

Expand Down Expand Up @@ -608,7 +612,12 @@ const CrosswordProvider = React.forwardRef<
return false;
}

// If we try to move to a cell with a direction it doesn't support,
// switch to the other direction. There is no codepath that can test
// this, though, as this callback isn't exposed, and we only call it in
// ways that guarantee that direction is valid.
if (!candidate[direction]) {
/* istanbul ignore next */
direction = otherDirection(direction);
}

Expand Down
2 changes: 1 addition & 1 deletion src/__test__/CrosswordProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ describe('imperative commands', () => {
expect(ref.current).toBeTruthy();
let isCorrect = true;
act(() => {
isCorrect = ref.current?.isCrosswordCorrect();
isCorrect = ref.current!.isCrosswordCorrect();
});

expect(isCorrect).toBeFalsy();
Expand Down
1 change: 1 addition & 0 deletions src/__test__/helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

// dummy to make "use default export!" warning go away.
/* istanbul ignore next */
export function NYI() {
// eslint-disable-next-line no-console
console.log('NYI!');
Expand Down
197 changes: 197 additions & 0 deletions src/__test__/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,25 @@ const simpleData = {
},
};

const simpleRectangularData = {
across: {
1: {
clue: 'one plus one',
answer: 'TWO',
row: 0,
col: 0,
},
},
down: {
2: {
clue: 'opposite of "off"',
answer: 'ON',
row: 0,
col: 2,
},
},
};

describe('calculateExtents()', () => {
it('applies across length to col', () => {
const result = calculateExtents(simpleData, 'across');
Expand Down Expand Up @@ -212,6 +231,184 @@ describe('createGridData()', () => {
expect(gridData).toEqual(expectedData);
expect(clues).toEqual(expectedClues);
});

it('creates square grid data by default', () => {
const { rows, cols, gridData, clues } = createGridData(
simpleRectangularData
);

const expectedData: GridData = [
[
{
row: 0,
col: 0,
used: true,
number: '1',
answer: 'T',
across: '1',
},
{
row: 0,
col: 1,
used: true,
answer: 'W',
across: '1',
},
{
row: 0,
col: 2,
used: true,
number: '2',
answer: 'O',
across: '1',
down: '2',
},
],
[
{
row: 1,
col: 0,
used: false,
},
{
row: 1,
col: 1,
used: false,
},
{
row: 1,
col: 2,
used: true,
answer: 'N',
down: '2',
},
],
[
{
row: 2,
col: 0,
used: false,
},
{
row: 2,
col: 1,
used: false,
},
{
row: 2,
col: 2,
used: false,
},
],
];

const expectedClues = {
across: [
{
number: '1',
clue: simpleRectangularData.across[1].clue,
row: 0,
col: 0,
answer: 'TWO',
},
],
down: [
{
number: '2',
clue: simpleRectangularData.down[2].clue,
row: 0,
col: 2,
answer: 'ON',
},
],
};

expect(rows).toBe(3);
expect(cols).toBe(3);
expect(gridData).toEqual(expectedData);
expect(clues).toEqual(expectedClues);
});

it('creates rectangular grid data when requested', () => {
const { rows, cols, gridData, clues } = createGridData(
simpleRectangularData,
true
);

const expectedData: GridData = [
[
{
row: 0,
col: 0,
used: true,
number: '1',
answer: 'T',
across: '1',
},
{
row: 0,
col: 1,
used: true,
answer: 'W',
across: '1',
},
{
row: 0,
col: 2,
used: true,
number: '2',
answer: 'O',
across: '1',
down: '2',
},
],
[
{
row: 1,
col: 0,
used: false,
},
{
row: 1,
col: 1,
used: false,
},
{
row: 1,
col: 2,
used: true,
answer: 'N',
down: '2',
},
],
];

const expectedClues = {
across: [
{
number: '1',
clue: simpleRectangularData.across[1].clue,
row: 0,
col: 0,
answer: 'TWO',
},
],
down: [
{
number: '2',
clue: simpleRectangularData.down[2].clue,
row: 0,
col: 2,
answer: 'ON',
},
],
};

expect(rows).toBe(2);
expect(cols).toBe(3);
expect(gridData).toEqual(expectedData);
expect(clues).toEqual(expectedClues);
});
});

describe('fillClues()', () => {
Expand Down

0 comments on commit fda3353

Please sign in to comment.