import {expect, test} from '@jest/globals'; import { generateCoilNoPrefix } from './coilNo'; describe('generateCoilNoPrefix', () => { // 保存原始的Date方法 let originalGetFullYear; let originalGetMonth; let originalGetDate; beforeEach(() => { // 保存原始方法 originalGetFullYear = Date.prototype.getFullYear; originalGetMonth = Date.prototype.getMonth; originalGetDate = Date.prototype.getDate; }); afterEach(() => { // 恢复原始方法 Date.prototype.getFullYear = originalGetFullYear; Date.prototype.getMonth = originalGetMonth; Date.prototype.getDate = originalGetDate; }); test("生成2026年3月3日的钢卷号前五位", () => { // 模拟日期:2026年3月3日 Date.prototype.getFullYear = jest.fn(() => 2026); Date.prototype.getMonth = jest.fn(() => 2); // 3月是索引2 Date.prototype.getDate = jest.fn(() => 3); const result = generateCoilNoPrefix(); expect(result).toBe('26L03'); }); test('生成2026年1月1日的钢卷号前五位', () => { // 模拟日期:2026年1月1日 Date.prototype.getFullYear = jest.fn(() => 2026); Date.prototype.getMonth = jest.fn(() => 0); // 1月是索引0 Date.prototype.getDate = jest.fn(() => 1); const result = generateCoilNoPrefix(); expect(result).toBe('26J01'); }); test('生成2026年12月31日的钢卷号前五位', () => { // 模拟日期:2026年12月31日 Date.prototype.getFullYear = jest.fn(() => 2026); Date.prototype.getMonth = jest.fn(() => 11); // 12月是索引11 Date.prototype.getDate = jest.fn(() => 31); const result = generateCoilNoPrefix(); expect(result).toBe('26U31'); }); test('生成2027年5月15日的钢卷号前五位', () => { // 模拟日期:2027年5月15日 Date.prototype.getFullYear = jest.fn(() => 2027); Date.prototype.getMonth = jest.fn(() => 4); // 5月是索引4 Date.prototype.getDate = jest.fn(() => 15); const result = generateCoilNoPrefix(); expect(result).toBe('27N15'); }); test('钢卷号前五位长度为5', () => { // 模拟任意日期 Date.prototype.getFullYear = jest.fn(() => 2026); Date.prototype.getMonth = jest.fn(() => 2); Date.prototype.getDate = jest.fn(() => 3); const result = generateCoilNoPrefix(); expect(result.length).toBe(5); }); test('钢卷号前五位格式正确', () => { // 模拟任意日期 Date.prototype.getFullYear = jest.fn(() => 2026); Date.prototype.getMonth = jest.fn(() => 2); Date.prototype.getDate = jest.fn(() => 3); const result = generateCoilNoPrefix(); // 格式:年份后两位(2位数字) + 月份字母(1位字母) + 日期两位(2位数字) const regex = /^\d{2}[JKLMNOPQRSTU]\d{2}$/; expect(regex.test(result)).toBe(true); }); });