2020-08-04 23:24:56 +02:00
|
|
|
/* eslint-env jest */
|
|
|
|
import { attachReactRefresh } from 'next/dist/build/webpack-config'
|
2021-01-26 20:28:41 +01:00
|
|
|
import * as storybookPlugin from '../../packages/next-plugin-storybook/preset'
|
|
|
|
|
|
|
|
describe('next-plugin-storybook filterModuleRules', () => {
|
|
|
|
it('should filter module rules correctly', async () => {
|
|
|
|
const input = {
|
|
|
|
module: { rules: [{ test: 'babel-loader' }, { test: /.*\.css/ }] },
|
|
|
|
}
|
|
|
|
const expected = [{ test: 'babel-loader' }]
|
|
|
|
|
|
|
|
const output = storybookPlugin.filterModuleRules(input)
|
|
|
|
expect(output).toEqual(expected)
|
|
|
|
})
|
|
|
|
})
|
2020-08-04 23:24:56 +02:00
|
|
|
|
|
|
|
describe('webpack-config attachReactRefresh', () => {
|
|
|
|
it('should skip adding when unrelated', () => {
|
|
|
|
const input = { module: { rules: [{ use: 'a' }] } }
|
|
|
|
const expected = { module: { rules: [{ use: 'a' }] } }
|
|
|
|
|
|
|
|
attachReactRefresh(input, 'rr')
|
|
|
|
expect(input).toEqual(expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should skip adding when existing (shorthand)', () => {
|
|
|
|
const input = {
|
|
|
|
module: {
|
2022-01-17 16:17:22 +01:00
|
|
|
rules: [
|
|
|
|
{
|
2022-05-31 02:05:27 +02:00
|
|
|
use: [
|
|
|
|
'next/dist/compiled/@next/react-refresh-utils/dist/loader',
|
|
|
|
'rr',
|
|
|
|
],
|
2022-01-17 16:17:22 +01:00
|
|
|
},
|
|
|
|
],
|
2020-08-04 23:24:56 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
const expected = {
|
|
|
|
module: {
|
2022-01-17 16:17:22 +01:00
|
|
|
rules: [
|
|
|
|
{
|
2022-05-31 02:05:27 +02:00
|
|
|
use: [
|
|
|
|
'next/dist/compiled/@next/react-refresh-utils/dist/loader',
|
|
|
|
'rr',
|
|
|
|
],
|
2022-01-17 16:17:22 +01:00
|
|
|
},
|
|
|
|
],
|
2020-08-04 23:24:56 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
attachReactRefresh(input, 'rr')
|
|
|
|
expect(input).toEqual(expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should skip adding when existing (longhand)', () => {
|
|
|
|
const input = {
|
|
|
|
module: {
|
|
|
|
rules: [
|
2022-01-17 16:17:22 +01:00
|
|
|
{
|
|
|
|
use: [
|
|
|
|
require.resolve(
|
2022-05-31 02:05:27 +02:00
|
|
|
'next/dist/compiled/@next/react-refresh-utils/dist/loader'
|
2022-01-17 16:17:22 +01:00
|
|
|
),
|
|
|
|
'rr',
|
|
|
|
],
|
|
|
|
},
|
2020-08-04 23:24:56 +02:00
|
|
|
],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
const expected = {
|
|
|
|
module: {
|
|
|
|
rules: [
|
2022-01-17 16:17:22 +01:00
|
|
|
{
|
|
|
|
use: [
|
|
|
|
require.resolve(
|
2022-05-31 02:05:27 +02:00
|
|
|
'next/dist/compiled/@next/react-refresh-utils/dist/loader'
|
2022-01-17 16:17:22 +01:00
|
|
|
),
|
|
|
|
'rr',
|
|
|
|
],
|
|
|
|
},
|
2020-08-04 23:24:56 +02:00
|
|
|
],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
attachReactRefresh(input, 'rr')
|
|
|
|
expect(input).toEqual(expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should add when missing (single, non-array)', () => {
|
|
|
|
const input = {
|
|
|
|
module: {
|
|
|
|
rules: [{ use: 'rr' }],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
attachReactRefresh(input, 'rr')
|
|
|
|
expect(input).toMatchObject({
|
|
|
|
module: {
|
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
use: [
|
2022-05-31 02:05:27 +02:00
|
|
|
expect.stringMatching(
|
|
|
|
/react-refresh-utils[\\/]dist[\\/]loader\.js/
|
|
|
|
),
|
2020-08-04 23:24:56 +02:00
|
|
|
'rr',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should add when missing (single, array)', () => {
|
|
|
|
const input = {
|
|
|
|
module: {
|
|
|
|
rules: [{ use: ['rr'] }],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
attachReactRefresh(input, 'rr')
|
|
|
|
expect(input).toMatchObject({
|
|
|
|
module: {
|
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
use: [
|
2022-05-31 02:05:27 +02:00
|
|
|
expect.stringMatching(
|
|
|
|
/react-refresh-utils[\\/]dist[\\/]loader\.js/
|
|
|
|
),
|
2020-08-04 23:24:56 +02:00
|
|
|
'rr',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should add when missing (before, array)', () => {
|
|
|
|
const input = {
|
|
|
|
module: {
|
|
|
|
rules: [{ use: ['bla', 'rr'] }],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
attachReactRefresh(input, 'rr')
|
|
|
|
expect(input).toMatchObject({
|
|
|
|
module: {
|
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
use: [
|
|
|
|
'bla',
|
2022-05-31 02:05:27 +02:00
|
|
|
expect.stringMatching(
|
|
|
|
/react-refresh-utils[\\/]dist[\\/]loader\.js/
|
|
|
|
),
|
2020-08-04 23:24:56 +02:00
|
|
|
'rr',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should add when missing (after, array)', () => {
|
|
|
|
const input = {
|
|
|
|
module: {
|
|
|
|
rules: [{ use: ['rr', 'bla'] }],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
attachReactRefresh(input, 'rr')
|
|
|
|
expect(input).toMatchObject({
|
|
|
|
module: {
|
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
use: [
|
2022-05-31 02:05:27 +02:00
|
|
|
expect.stringMatching(
|
|
|
|
/react-refresh-utils[\\/]dist[\\/]loader\.js/
|
|
|
|
),
|
2020-08-04 23:24:56 +02:00
|
|
|
'rr',
|
|
|
|
'bla',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should add when missing (multi, array)', () => {
|
|
|
|
const input = {
|
|
|
|
module: {
|
|
|
|
rules: [{ use: ['hehe', 'haha', 'rawr', 'rr', 'lol', 'bla'] }],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
attachReactRefresh(input, 'rr')
|
|
|
|
expect(input).toMatchObject({
|
|
|
|
module: {
|
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
use: [
|
|
|
|
'hehe',
|
|
|
|
'haha',
|
|
|
|
'rawr',
|
2022-05-31 02:05:27 +02:00
|
|
|
expect.stringMatching(
|
|
|
|
/react-refresh-utils[\\/]dist[\\/]loader\.js/
|
|
|
|
),
|
2020-08-04 23:24:56 +02:00
|
|
|
'rr',
|
|
|
|
'lol',
|
|
|
|
'bla',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should skip when present (multi, array)', () => {
|
|
|
|
const input = {
|
|
|
|
module: {
|
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
use: [
|
|
|
|
'hehe',
|
|
|
|
'haha',
|
2022-05-31 02:05:27 +02:00
|
|
|
'next/dist/compiled/@next/react-refresh-utils/dist/loader',
|
2020-08-04 23:24:56 +02:00
|
|
|
'rr',
|
|
|
|
'lol',
|
|
|
|
'bla',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
attachReactRefresh(input, 'rr')
|
|
|
|
expect(input).toMatchObject({
|
|
|
|
module: {
|
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
use: [
|
|
|
|
'hehe',
|
|
|
|
'haha',
|
2022-05-31 02:05:27 +02:00
|
|
|
'next/dist/compiled/@next/react-refresh-utils/dist/loader',
|
2020-08-04 23:24:56 +02:00
|
|
|
'rr',
|
|
|
|
'lol',
|
|
|
|
'bla',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should skip when present (multi, array, wrong order)', () => {
|
|
|
|
const input = {
|
|
|
|
module: {
|
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
use: [
|
|
|
|
'hehe',
|
|
|
|
'haha',
|
|
|
|
'rr',
|
|
|
|
'lol',
|
2022-05-31 02:05:27 +02:00
|
|
|
'next/dist/compiled/@next/react-refresh-utils/dist/loader',
|
2020-08-04 23:24:56 +02:00
|
|
|
'bla',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
attachReactRefresh(input, 'rr')
|
|
|
|
expect(input).toMatchObject({
|
|
|
|
module: {
|
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
use: [
|
|
|
|
'hehe',
|
|
|
|
'haha',
|
|
|
|
'rr',
|
|
|
|
'lol',
|
2022-05-31 02:05:27 +02:00
|
|
|
'next/dist/compiled/@next/react-refresh-utils/dist/loader',
|
2020-08-04 23:24:56 +02:00
|
|
|
'bla',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|