summary refs log tree commit diff stats
path: root/javascript/luhn/luhn.spec.js
blob: 614348ed40874853d38667c28c0d68d9c1782756 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { valid } from './luhn';

describe('Luhn', () => {
  test('single digit strings can not be valid', () => {
    expect(valid('1')).toEqual(false);
  });

  test('a single zero is invalid', () => {
    expect(valid('0')).toEqual(false);
  });

  test('a simple valid SIN that remains valid if reversed', () => {
    expect(valid('059')).toEqual(true);
  });

  test('a simple valid SIN that becomes invalid if reversed', () => {
    expect(valid('59')).toEqual(true);
  });

  test('a valid Canadian SIN', () => {
    expect(valid('055 444 285')).toEqual(true);
  });

  test('invalid Canadian SIN', () => {
    expect(valid('055 444 286')).toEqual(false);
  });

  test('invalid credit card', () => {
    expect(valid('8273 1232 7352 0569')).toEqual(false);
  });

  test('invalid long number with an even remainder', () => {
    expect(valid('1 2345 6789 1234 5678 9012')).toEqual(false);
  });

  test('valid number with an even number of digits', () => {
    expect(valid('095 245 88')).toEqual(true);
  });

  test('valid number with an odd number of spaces', () => {
    expect(valid('234 567 891 234')).toEqual(true);
  });

  test('valid strings with a non-digit added at the end invalid', () => {
    expect(valid('059a')).toEqual(false);
  });

  test('valid strings with punctuation included become invalid', () => {
    expect(valid('055-444-285')).toEqual(false);
  });

  test('valid strings with symbols included become invalid', () => {
    expect(valid('055# 444$ 285')).toEqual(false);
  });

  test('single zero with space is invalid', () => {
    expect(valid(' 0')).toEqual(false);
  });

  test('more than a single zero is valid', () => {
    expect(valid('0000 0')).toEqual(true);
  });

  test('input digit 9 is correctly converted to output digit 9', () => {
    expect(valid('091')).toEqual(true);
  });

  test("using ascii value for non-doubled non-digit isn't allowed", () => {
    expect(valid('055b 444 285')).toEqual(false);
  });

  test("using ascii value for doubled non-digit isn't allowed", () => {
    expect(valid(':9')).toEqual(false);
  });
});