• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

cartesi / rollups-explorer / 10491533758

21 Aug 2024 02:11PM CUT coverage: 93.391% (-0.4%) from 93.766%
10491533758

Pull #232

github

nevendyulgerov
test(apps/web): Add unit tests for specifications v1 validator
Pull Request #232: #229 Add import and export for specifications

1211 of 1433 branches covered (84.51%)

Branch coverage included in aggregate %.

487 of 570 new or added lines in 9 files covered. (85.44%)

50 existing lines in 10 files now uncovered.

12863 of 13637 relevant lines covered (94.32%)

45.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

48.63
/apps/web/src/components/specification/hooks/useSpecificationsTransfer.tsx
1
"use client";
1✔
2

1✔
3
import { ReactNode, useCallback, useMemo } from "react";
1✔
4
import { notifications } from "@mantine/notifications";
1✔
5
import { useSpecification } from "./useSpecification";
1✔
6
import {
1✔
7
    Specification,
1✔
8
    SpecificationTransfer as SpecificationTransferModel,
1✔
9
    ValidationType,
1✔
10
    SPECIFICATION_TRANSFER_NAME,
1✔
11
} from "../types";
1✔
12
import {
1✔
13
    validateSpecification,
1✔
14
    VALIDATOR_VERSION,
1✔
15
} from "../transfer/validator";
1✔
16

1✔
17
export const useSpecificationsTransfer = () => {
1✔
18
    const { listSpecifications, addSpecification } = useSpecification();
31✔
19
    const specifications = listSpecifications();
31✔
20
    const version = Number(VALIDATOR_VERSION);
31✔
21
    const specificationExport: SpecificationTransferModel = useMemo(
31✔
22
        () => ({
31✔
23
            name: SPECIFICATION_TRANSFER_NAME,
14✔
24
            version,
14✔
25
            timestamp: new Date().getTime(),
14✔
26
            specifications: specifications ?? [],
14✔
27
        }),
14✔
28
        [specifications, version],
31✔
29
    );
31✔
30

31✔
31
    const exportLink = useMemo(
31✔
32
        () =>
31✔
33
            `data:text/json;charset=utf-8,${encodeURIComponent(
14✔
34
                JSON.stringify(specificationExport, null, 4),
14✔
35
            )}`,
14✔
36
        [specificationExport],
31✔
37
    );
31✔
38

31✔
39
    const displayAlert = useCallback(
31✔
40
        (message: ReactNode, type: ValidationType = "error") => {
31✔
NEW
41
            const isSuccess = type === "success";
×
NEW
42
            notifications.show({
×
NEW
43
                message,
×
NEW
44
                color: isSuccess ? "green" : "red",
×
NEW
45
                withBorder: true,
×
NEW
46
                autoClose: isSuccess,
×
NEW
47
            });
×
NEW
48
        },
×
49
        [],
31✔
50
    );
31✔
51

31✔
52
    const onValidateSuccessfully = useCallback(
31✔
53
        (specificationImport: SpecificationTransferModel) => {
31✔
NEW
54
            Promise.all(
×
NEW
55
                specificationImport.specifications.map(
×
NEW
56
                    (specification: Specification) =>
×
NEW
57
                        new Promise((resolve, reject) => {
×
NEW
58
                            setTimeout(() =>
×
NEW
59
                                addSpecification(specification, {
×
NEW
60
                                    onSuccess: () => resolve(undefined),
×
NEW
61
                                    onFailure: (err) => reject(err),
×
NEW
62
                                }),
×
NEW
63
                            );
×
NEW
64
                        }),
×
NEW
65
                ),
×
NEW
66
            )
×
NEW
67
                .then(() =>
×
NEW
68
                    displayAlert(
×
NEW
69
                        "Specifications were imported successfully.",
×
NEW
70
                        "success",
×
NEW
71
                    ),
×
NEW
72
                )
×
NEW
73
                .catch((err) =>
×
NEW
74
                    displayAlert(
×
NEW
75
                        `Unable to import specifications. Error is: ${
×
NEW
76
                            err?.message ?? err
×
NEW
77
                        }`,
×
NEW
78
                    ),
×
NEW
79
                );
×
NEW
80
        },
×
81
        [addSpecification, displayAlert],
31✔
82
    );
31✔
83

31✔
84
    const readFileContent = useCallback(
31✔
85
        (file: File) => {
31✔
NEW
86
            const fileReader = new FileReader();
×
NEW
87

×
NEW
88
            fileReader.onload = () => {
×
NEW
89
                try {
×
NEW
90
                    const specificationImport = JSON.parse(
×
NEW
91
                        fileReader.result as string,
×
NEW
92
                    );
×
NEW
93

×
NEW
94
                    validateSpecification(specificationImport)
×
NEW
95
                        .then(() => onValidateSuccessfully(specificationImport))
×
NEW
96
                        .catch((errors: string[]) => {
×
NEW
97
                            displayAlert(
×
NEW
98
                                <>
×
NEW
99
                                    <div>
×
NEW
100
                                        The imported file is not valid. Please
×
NEW
101
                                        check the following errors:
×
NEW
102
                                    </div>
×
NEW
103
                                    <div>
×
NEW
104
                                        {errors.map((error) => (
×
NEW
105
                                            <div key={error}>{error}</div>
×
NEW
106
                                        ))}
×
NEW
107
                                    </div>
×
NEW
108
                                </>,
×
NEW
109
                            );
×
NEW
110
                        });
×
NEW
111
                } catch (err) {
×
NEW
112
                    displayAlert("Unable to parse specification import.");
×
NEW
113
                }
×
NEW
114
            };
×
NEW
115
            fileReader.onerror = () =>
×
NEW
116
                displayAlert(
×
NEW
117
                    "Unable to import file. File content may not be readable.",
×
NEW
118
                );
×
NEW
119

×
NEW
120
            fileReader.readAsText(file);
×
NEW
121
        },
×
122
        [displayAlert, onValidateSuccessfully],
31✔
123
    );
31✔
124

31✔
125
    const onChangeFile = useCallback(
31✔
126
        (file: File | null) => {
31✔
NEW
127
            if (file) {
×
NEW
128
                readFileContent(file);
×
NEW
129
            }
×
NEW
130
        },
×
131
        [readFileContent],
31✔
132
    );
31✔
133

31✔
134
    return useMemo(
31✔
135
        () => ({
31✔
136
            exportLink,
31✔
137
            onChangeFile,
31✔
138
        }),
31✔
139
        [exportLink, onChangeFile],
31✔
140
    );
31✔
141
};
31✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc