mass changes

This commit is contained in:
Joshua 2024-01-29 19:08:06 +01:00
parent b61e5c0552
commit 5db74ebcb6
23 changed files with 356 additions and 358 deletions

2
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,2 @@
{
}

View file

@ -1,14 +1,15 @@
const { defineConfig } = require("cypress"); const { defineConfig } = require("cypress");
const { functions } = require("./src/utils");
module.exports = defineConfig({ module.exports = defineConfig({
snapshot: { snapshot: {
// snapshotPath: "cypress/snapshots/", // snapshotPath: "cypress/snapshots/",
SNAPSHOT_UPDATE: true // SNAPSHOT_UPDATE: true,
useFolders: true,
}, },
e2e: { e2e: {
setupNodeEvents(on, config) { setupNodeEvents(on, config) {
functions.tasks(on, config); // implement node event listeners here
}, },
}, },
}); });

View file

@ -1,75 +1,78 @@
/* eslint-env mocha */ /* eslint-env mocha */
/* global cy */ /* global cy */
describe("@datashard/snapshot", () => { describe("datashard/snapshot", () => {
context("simple types", () => { context("simple types"
// , { env: { SNAPSHOT_UPDATE: true } }
, () => {
it("works with objects", () => { it("works with objects", () => {
cy.wrap({ cy.wrap({
"foo": "bar", "foo": "bar",
"Fizzy Drink": "Pop" "Fizzy Drink": "Pop"
}).snapshot( }).snapshot();
// "Filename", { });
// snapshotPath: "asdasd"
// } it("works with numbers", () => {
cy.wrap(42).snapshot({
snapshotPath: "cypress/fixtures/snapshots",
snapshotName: "Numbers",
});
});
it("works with strings", () => {
cy.wrap("foo-bar").snapshot({
snapshotPath: "cypress/fixtures/snapshots",
snapshotName: "Strings",
});
});
it(
"works with arrays",
{
env: {
SNAPSHOT_UPDATE: true,
},
},
() => {
cy.wrap([1, 2, 3, 4]).snapshot({
snapshotPath: "cypress/fixtures/snapshots",
snapshotName: "Arrays",
});
}
); );
// cypress/fixtures/@datashard-snapshot/simple-types/works-with-objects/asdjskadhasj.json
}); });
context("complex types"
// it("works with numbers", () => { // , { env: { SNAPSHOT_UPDATE: true } }
// cy.wrap(42).snapshot({ , () => {
// snapshotPath: "cypress/fixtures/snapshots", it('works with more "complicated" Objects', () => {
// snapshotName: "Numbers", cy.wrap({
// }); "status": 200,
// }); "response": {
"array": [0, 1, 2, "Three"],
// it("works with strings", () => { "object": {
// cy.wrap("foo-bar").snapshot({ "with": "more details"
// snapshotPath: "cypress/fixtures/snapshots", }
// snapshotName: "Strings", }
// }); }
// }); ).snapshot()
})
// it( it("works based on fixtures", () => {
// "works with arrays", cy
// { .wrap({
// env: { "jsonapi": {
// SNAPSHOT_UPDATE: true, "version": "2.0"
// }, },
// }, "included": [
// () => { {
// cy.wrap([1, 2, 3, 4]).snapshot({ "type": "users",
// snapshotPath: "cypress/fixtures/snapshots", "id": "2",
// snapshotName: "Arrays", "attributes": {
// }); "name": "Test"
// } }
// ); }
// it('works with more "complicated" Objects', () => { ]
// cy.fixture("Complex").snapshot({ })
// snapshotPath: 'cypress/fixtures/snapshots', .snapshot();
// snapshotName: "Complex"
// })
// })
// it.only("works based on fixtures", () => {
// cy
// .wrap({
// "jsonapi": {
// "version": "2.0"
// },
// "included": [
// {
// "type": "users",
// "id": "2",
// "attributes": {
// "name": "Test"
// }
// }
// ]
// })
// .snapshot({
// snapshotFixture: "generated",
// // snapshotPath: "cypress/fixtures/snapshots",
// // snapshotName: "generated",
// });
// });
}); });
})
}); });

View file

@ -1,4 +0,0 @@
{
"foo": "bar",
"Fizzy Drink": "Soda"
}

View file

@ -1,4 +0,0 @@
{
"foo": "bar",
"Fizzy Drink": "Pop"
}

View file

@ -0,0 +1,14 @@
{
"jsonapi": {
"version": "2.0"
},
"included": [
{
"type": "users",
"id": "2",
"attributes": {
"name": "Test"
}
}
]
}

View file

@ -0,0 +1,14 @@
{
"jsonapi": {
"version": "2.0"
},
"included": [
{
"type": "users",
"id": "2",
"attributes": {
"name": "Test"
}
}
]
}

View file

@ -1,7 +1,12 @@
{ {
"status": 200, "status": 200,
"response": { "response": {
"array": [0, 1, 2, "Three"], "array": [
0,
1,
2,
"Three"
],
"object": { "object": {
"with": "more details" "with": "more details"
} }

View file

@ -1 +0,0 @@
{"foo":"bar","Fizzy Drink":"Soda"}

View file

@ -1,4 +0,0 @@
{
"foo": "bar",
"Fizzy Drink": "Soda"
}

View file

@ -1,2 +1,2 @@
// register .snapshot() command // register .snapshot() command
require('../..').register() require('../../src/index').register()

View file

@ -1,7 +1,6 @@
// global cy, Cypress // global cy, Cypress
const { functions } = require("./utils/index"); const register = require("./register");
module.exports = { module.exports = {
register: functions.register, register
tasks: functions.tasks,
}; };

View file

@ -1,6 +1,6 @@
const lazy = require("lazy-ass"); const lazy = require("lazy-ass");
const is = require("check-more-types"); const is = require("check-more-types");
const snapshot = require("../snapshots/snapshot"); const snapshot = require("./snapshot");
module.exports = () => { module.exports = () => {
lazy(is.fn(global.before), "Missing global before function"); lazy(is.fn(global.before), "Missing global before function");

View file

@ -1,6 +1,6 @@
const serializeDomElement = require("../serializers/serializeDomElement"); const serializeDomElement = require("./utils/serializers/serializeDomElement");
const serializeToHTML = require("../serializers/serializeToHTML"); const serializeToHTML = require("./utils/serializers/serializeToHTML");
const compareValues = require("./compareValues"); const compareValues = require("./utils/compareValues");
const path = require("path"); const path = require("path");
const identity = (x) => x; const identity = (x) => x;
@ -12,24 +12,15 @@ const pickSerializer = (asJson, value) => {
}; };
const store_snapshot = (props = { value, name, raiser }) => { const store_snapshot = (props = { value, name, raiser }) => {
if (!Cypress.env().SNAPSHOT_UPDATE) { if (Cypress.env().SNAPSHOT_UPDATE || Cypress.config('snapshot').SNAPSHOT_UPDATE) {
cy.fixture(props.name).then(content => props.raiser({ value: props.value, expected: content })) cy.log(props.name)
console.log(props.name)
cy.writeFile(`${props.name}.json`, JSON.stringify(props.value, null, 2))
} else { } else {
cy.writeFile(`${props.name}.json`, JSON.stringify(props.value.null, 2)) // TODO: Figure out how to replace the fixture folder name if people move it
const fixtureName = props.name.replace("cypress/fixtures", "")
cy.fixture(fixtureName).then(content => props.raiser({ value: props.value, expected: content }))
} }
// cy.fixture(props.name)
// .then(exist => {
// cy.log('fixture 2')
// if (exist && !Cypress.env().SNAPSHOT_UPDATE) {
// cy.log(`fixture exists and doesn't update`)
// props.raiser({ value: props.value, expected: exist, type: "fixture" });
// } else {
// cy.log(`fixture exists and updates`)
// cy.writeFile(expectedPath, JSON.stringify(props.value, null, 2));
// }
// })
}; };
const set_snapshot = ({ snapshotName, serialized, value }) => { const set_snapshot = ({ snapshotName, serialized, value }) => {
@ -73,23 +64,47 @@ const set_snapshot = ({ snapshotName, serialized, value }) => {
}); });
}; };
function replaceCharacters(str, asFolder, sep) {
if (asFolder) {
if (!sep) throw new Error("Separator not Passed.")
return str
.replace(/ /gi, '-')
.replace(/\//gi, "-")
.replaceAll('"', '')
.replaceAll(sep, '/')
} else {
return str
.replaceAll(' ', '-')
.replaceAll('/', '-')
.replaceAll('"', '')
}
}
const get_snapshot_name = (asFolder, stepName) => { const get_snapshot_name = (asFolder, stepName) => {
const names = Cypress.currentTest.titlePath; const names = Cypress.currentTest.titlePath;
const sep = ">>datashard.work<<" const sep = ">>datashard.work<<"
if (stepName) names.push(stepName)
if (asFolder) return names.join(sep).replace(/ /gi, "-").replace(/\//gi, "-").replaceAll(sep, '/')
else return names.join('__').replaceAll(" ", "-").replaceAll("/", "-") if (stepName && typeof stepName !== 'object') {
names.push(stepName)
}
if (asFolder) return replaceCharacters(names.join(sep), true, sep)
else return replaceCharacters(names.join('__'), false)
}; };
module.exports = (value, stepName, options = { json: true, asFolder: false }) => { module.exports = (value, stepName, options = { json: true }) => {
if (typeof stepName === 'object') options = { ...options, ...stepName }
if (typeof value !== "object" || Array.isArray(value)) if (typeof value !== "object" || Array.isArray(value))
value = { data: value }; value = { data: value };
const serializer = pickSerializer(options.json, value); const serializer = pickSerializer(options.json, value);
const serialized = serializer(value); const serialized = serializer(value);
options.asFolder = Cypress.config('snapshot').useFolders || false
set_snapshot({ set_snapshot({
snapshotName: path.join( snapshotName: path.join(
options.snapshotPath || Cypress.config('snapshot').snapshotPath || 'snapshots', options.snapshotPath || Cypress.config('snapshot').snapshotPath || 'cypress/fixtures/snapshots',
`/${get_snapshot_name(options.asFolder, stepName)}`), `/${get_snapshot_name(options.asFolder, stepName)}`),
serialized, serialized,
value, value,

View file

@ -1,5 +0,0 @@
const readFileMaybe = require("../tasks/readFileMaybe");
module.exports = (on, config) => {
on("task", { readFileMaybe });
};

View file

@ -1,28 +0,0 @@
const serializeToHTML = require("./serializers/serializeToHTML");
const serializeDomElement = require("./serializers/serializeDomElement");
const compareValues = require("./snapshots/compareValues");
const readFileMaybe = require("./tasks/readFileMaybe");
const identity = (x) => x;
const publicProps = (name) => !name.startsWith("__");
const countSnapshots = (snapshots) =>
Object.keys(snapshots).filter(publicProps).length;
module.exports = {
serializers: {
serializeDomElement,
serializeToHTML,
identity,
countSnapshots,
},
snapshots: {
compareValues,
},
functions: {
register: require("./functions/register"),
tasks: require("./functions/addTasks"),
},
tasks: {
readFileMaybe,
},
};

View file

@ -1,9 +0,0 @@
const fs = require("fs");
module.exports = (filename) => {
if (fs.existsSync(filename)) {
return fs.readFileSync(filename, "utf8");
}
return false;
};